Jump to page: 1 2 3
Thread overview
How to setup D language with Apache httpd cgi?
Jul 30, 2019
BoQsc
Jul 30, 2019
Adam D. Ruppe
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
0xEAB
Jul 31, 2019
BoQsc
Jul 31, 2019
0xEAB
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
Andre Pany
Jul 31, 2019
BoQsc
Jul 31, 2019
Andre Pany
Jul 31, 2019
Adam D. Ruppe
Jul 31, 2019
Andre Pany
Jul 31, 2019
bachmeier
Jul 31, 2019
BoQsc
Jul 31, 2019
BoQsc
Jul 31, 2019
Andre Pany
Jul 31, 2019
BoQsc
Jul 31, 2019
H. S. Teoh
July 30, 2019
Hello, I would like to know how to setup D language project, so that it would work with Apache httpd cgi.

Do I need some kind of cgi library for D language? Right now all I'm getting is internal server error.

Btw. Unix Bash script examples seems to work well with with Apache httpd cgi, they do not show internal server error.


Before trying examples below, it is required that you install apache httpd:
> sudo apt-get install apache2

And enable cgi module
> sudo a2enmod cgi
> sudo service apache2 restart

Here are examples:

D language example, this one does not work. Give internal error.

/usr/lib/cgi-bin/example.d
>#!/usr/bin/env rdmd
>import std.stdio;
>void main()
>{
>    writeln("Hello, world with automated script running!");
>}


Bash example, this one working perfectly.

/usr/lib/cgi-bin/example.sh
>#!/bin/bash
>echo "Content-type: text/html"
>echo ''
>echo 'CGI Bash Example'


Also remember to chmod the examples, before opening them:
> sudo chmod 755 /usr/lib/cgi-bin/example.sh
> sudo chmod 755 /usr/lib/cgi-bin/example.d
July 30, 2019
On Tuesday, 30 July 2019 at 20:36:18 UTC, BoQsc wrote:
>>    writeln("Hello, world with automated script running!");

You didn't follow the cgi protocol here - you didn't output the content type and the required blank line to separate headers from content.

>>echo "Content-type: text/html"
>>echo ''
>>echo 'CGI Bash Example'

see how they did it here?


there are cgi libraries for D too (like my own) but for this simple case you just forgot a couple lines.
July 31, 2019
On Tuesday, 30 July 2019 at 21:55:00 UTC, Adam D. Ruppe wrote:
> the required blank line to separate headers from content.
>

That's exactly what causes the Internal Server Error.

This is a working example.d
>#!/usr/bin/env rdmd
>import std.stdio;
>void main()
>{
>
>    writeln("");
>
>
>}

And this one, even produce HTML content to the HTML body tag:
>#!/usr/bin/env rdmd
>import std.stdio;
>void main()
>{
>    writeln("Content-type: text/html");
>    writeln("");
>    writeln("CGI D Example");
>
>}

Thanks, Adam D. Ruppe, it works.
July 31, 2019
On Wednesday, 31 July 2019 at 05:56:46 UTC, BoQsc wrote:
> what causes the Internal Server Error.

Internal Server Error might as well appear when D language syntax is not correct.

Considering this: this example has Internal Server Error, since writeln argument and argument content cannot contain the same type quotes: Internal Server Error
>#!/usr/bin/env rdmd
>import std.stdio;
>void main()
>{
>    writeln("Content-type: text/html");
>    writeln("");
>    writeln("<body style="background: green">CGI D Example</body>");
>
>}

This can be solved by using single quotes in the argument content places
>#!/usr/bin/env rdmd
>import std.stdio;
>void main()
>{
>    writeln("Content-type: text/html");
>    writeln("");
>    writeln("<body style='background: green'>CGI D Example</body>");
>
>}


Or even escaping argument content quotes:
>#!/usr/bin/env rdmd
>import std.stdio;
>void main()
>{
>    writeln("Content-type: text/html");
>    writeln("");
>    writeln("<body style=\"background: green\">CGI D Example</body>");
>
>}

That is good to know.
July 31, 2019
On Wednesday, 31 July 2019 at 06:30:03 UTC, BoQsc wrote:
> This can be solved by using single quotes in the argument content places
>>#!/usr/bin/env rdmd
>>import std.stdio;
>>void main()
>>{
>>    writeln("Content-type: text/html");
>>    writeln("");
>>    writeln("<body style='background: green'>CGI D Example</body>");
>>
>>}

Does the job but is a bad fix.

Use `` quotes for the string literal instead.
Further info: https://dlang.org/spec/lex.html#wysiwyg


 - Elias
July 31, 2019
On Wednesday, 31 July 2019 at 06:30:03 UTC, BoQsc wrote:
> On Wednesday, 31 July 2019 at 05:56:46 UTC, BoQsc wrote:
>> what causes the Internal Server Error.
>
> Internal Server Error might as well appear when D language syntax is not correct.

Maybe you want to use some wrapper around rdmd that does output a proper CGI response on error. Since this error occurs because rdmd's error message as-is is not valid for CGI.


 - Elias
July 31, 2019
On Wednesday, 31 July 2019 at 06:52:46 UTC, 0xEAB wrote:
> On Wednesday, 31 July 2019 at 06:30:03 UTC, BoQsc wrote:
>> This can be solved by using single quotes in the argument content places
>>>#!/usr/bin/env rdmd
>>>import std.stdio;
>>>void main()
>>>{
>>>    writeln("Content-type: text/html");
>>>    writeln("");
>>>    writeln("<body style='background: green'>CGI D Example</body>");
>>>
>>>}
>
> Does the job but is a bad fix.
>
> Use `` quotes for the string literal instead.
> Further info: https://dlang.org/spec/lex.html#wysiwyg
>
>
>  - Elias

I wasn't aware of Wysiwyg Strings, thanks,
seems to work very well.

>#!/usr/bin/env rdmd
>import std.stdio;
>void main()
>{
>    writeln(`Content-type: text/html`);
>    writeln(``);
>    writeln(`<body style="background: lightgreen">CGI D Example</body>`);
>
>}


July 31, 2019
On Wednesday, 31 July 2019 at 06:55:06 UTC, 0xEAB wrote:
> On Wednesday, 31 July 2019 at 06:30:03 UTC, BoQsc wrote:
>> On Wednesday, 31 July 2019 at 05:56:46 UTC, BoQsc wrote:
>>> what causes the Internal Server Error.
>>
>> Internal Server Error might as well appear when D language syntax is not correct.
>
> Maybe you want to use some wrapper around rdmd that does output a proper CGI response on error. Since this error occurs because rdmd's error message as-is is not valid for CGI.
>
>
>  - Elias

I'm kind of new to everything, I'm not sure how this have to be done.
July 31, 2019
There are some other bad news, I switched from rdmd to dub package manager since I need a package from Adam D. Ruppe.

It is all good and well: this one works perfectly.
>#!/usr/bin/env dub
>/+ dub.sdl:
>	name "hello"
>	
>+/
>import std.stdio;
>void main()
>{
>    writeln(`Content-type: text/html`);
>    writeln(``);
>    writeln(`<body style="background: lightgreen">CGI D Example</body>`);
>
>
}


But, once I add dub dependency, the error appears: Internal Server Error
>#!/usr/bin/env dub
>/+ dub.sdl:
>	name "hello"
> 	dependency "arsd-official" version="~>4.0.1"
>	
>+/
>import std.stdio;
>void main()
>{
>    writeln(`Content-type: text/html`);
>    writeln(``);
>    writeln(`<body style="background: lightgreen">CGI D Example</body>`);
>
>
}

This seems to work well when running not from cgi, so there is no syntax error.
July 31, 2019
I tried to change shebang, but: Internal Server Error still appears.

>#!/usr/bin/env dub run --single
>/+ dub.sdl:
>	name "hello"
>	dependency "arsd-official" version="~>4.0.1"
>	
>+/
>import std.stdio;
>void main()
>{
>    writeln(`Content-type: text/html`);
>    writeln(``);
>    writeln(`<body style="background: lightgreen">CGI D Example</body>`);
>
>}

« First   ‹ Prev
1 2 3