July 31, 2019
On Wednesday, 31 July 2019 at 12:51:09 UTC, Adam D. Ruppe wrote:
> On Wednesday, 31 July 2019 at 09:09:12 UTC, BoQsc wrote:
>>>	dependency "arsd-official" version="~>4.0.1"
>
> I just changed the thing, so now you will want to use version 4.0.2 and also require the cgi configuration rather than the default (I don't know how to do that in dub).
>
> Though personally, when I do cgi stuff in D, I don't bother with rdmd or dub or whatever, I just compile the program separately and copy the binary in to the cgi directory. It is more efficient and simpler to handle in error cases.

/+ dub.sdl:
    name "application"
    dependency "arsd-official:cgi" version="4.0.2"
    subConfiguration "arsd-official:cgi" "cgi"
+/

Kind regards
Andre
July 31, 2019
On Wednesday, 31 July 2019 at 07:19:12 UTC, BoQsc wrote:
> 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.

I'm curious why you don't just compile the binary and put it in the CGI directory. That's what I've always done. This AFAICT (I don't often use Dub) is going to compile every time it runs, which makes the program unnecessarily slow, and if it's used heavily, will add quite a load to the server. If you use Adam's cgi.d, you can test your program at the command line, and even run a server on localhost to test it in your browser.
July 31, 2019
> is going to compile every time it runs, which makes the program unnecessarily slow, and if it's used heavily, will add quite a load to the server.

I finally done it, and I'm not sure if it compiles every time. It opens the page lightning fast since I use SSD drive. I'm not sure how to check if it compiles every time.

Right now, I'm logged in as root and chmoded recursively the whole /usr/lib/cgi-bin/ folder
and it succeed.


>#!/usr/bin/env dub /+ dub.sdl:
>    name "application"
>    dependency "arsd-official:dom" version="4.0.2"
>    subConfiguration "arsd-official:cgi" "cgi"
>+/
>import std.stdio;
>import arsd.dom;
>void main()
>{
>    writeln(`Content-type: text/html`);
>    writeln(``);
> 
>    auto document = new Document("<html><body><p>paragraph</p></body></html>");
>    document.root.innerHTML = "<p>hey</p>";
>    writeln(document);
>
>//    writeln(`<body style="background: lightgreen">CGI D Example</body>`);
>
>}
July 31, 2019
However I tried to add options (--single) to the dub shebang and apache now throwing: "bad header error"

Without "--single" option seems to work.


>#!/usr/bin/env -vS dub --single
>/+ dub.sdl:
>    name "application"
>    dependency "arsd-official:dom" version="4.0.2"
>+/
>import std.stdio;
>import arsd.dom;
>void main()
>{
>    writeln(`Content-type: text/html`);
>    writeln(``);
>    writeln(`test`);
>
>}



>split -S:  'dub --single'
> into:    'dub'
>     &    '--single'
>executing: dub
>   arg[0]= 'dub'
>   arg[1]= '--single'
>   arg[2]= '/usr/lib/cgi-bin/example.d'
>[Wed Jul 31 19:57:56.892522 2019] [cgid:error] [pid 833:tid 140583783876352] [client >127.0.0.1:43598] malformed header from script 'example.d': Bad header: Performing "debug" >build using
July 31, 2019
On Wednesday, 31 July 2019 at 14:19:20 UTC, bachmeier wrote:

> is going to compile every time it runs, which makes the program unnecessarily slow

If only I could add
>dub --single --rdmd
to the shebang, I think dub might stop compiling every time.

However as pointed out in the above post, I'm unable to use options in shebang since apache is throwing header error for unknown reasons.


July 31, 2019
On Wed, Jul 31, 2019 at 05:09:50PM +0000, BoQsc via Digitalmars-d-learn wrote: [...]
> However as pointed out in the above post, I'm unable to use options in shebang since apache is throwing header error for unknown reasons.
[...]

Usually, that's a sign that somebody is writing to stdout/stderr that should not be, because apache usually redirects stdout to the actual HTTP output stream.  So if dub writes to stdout from within an apache module, that will garble the HTTP output stream and cause header errors and other kinds of errors.  You may need to somehow redirect stdout/stderr if this is the case.


T

-- 
We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare.  Now, thanks to the Internet, we know this is not true. -- Robert Wilensk
July 31, 2019
On Wednesday, 31 July 2019 at 17:04:58 UTC, BoQsc wrote:
> However I tried to add options (--single) to the dub shebang and apache now throwing: "bad header error"
>
> Without "--single" option seems to work.
>
>
>>#!/usr/bin/env -vS dub --single
>>/+ dub.sdl:
>>    name "application"
>>    dependency "arsd-official:dom" version="4.0.2"
>>+/
>>import std.stdio;
>>import arsd.dom;
>>void main()
>>{
>>    writeln(`Content-type: text/html`);
>>    writeln(``);
>>    writeln(`test`);
>>
>>}
>
>
>
>>split -S:  'dub --single'
>> into:    'dub'
>>     &    '--single'
>>executing: dub
>>   arg[0]= 'dub'
>>   arg[1]= '--single'
>>   arg[2]= '/usr/lib/cgi-bin/example.d'
>>[Wed Jul 31 19:57:56.892522 2019] [cgid:error] [pid 833:tid 140583783876352] [client >127.0.0.1:43598] malformed header from script 'example.d': Bad header: Performing "debug" >build using

As already pointed out, as starting point you could compile the application e.g. within folder /tmp and then copy the executable to cgi-bin folder.
Please make sure it has executable flag.

dub build --single hello.d

The application could look like this:
#!/usr/bin/env dub

/+ dub.sdl:
    name "application"
    dependency "arsd-official:cgi" version="4.0.2"
    subConfiguration "arsd-official:cgi" "cgi"
+/
import arsd.cgi;

void hello(Cgi cgi) {                                                                  cgi.setResponseContentType("text/plain");

    if("name" in cgi.get)
        cgi.write("Hello, " ~ cgi.get["name"]);
    else
        cgi.write("Hello, world!");
 }

mixin GenericMain!hello;

Kind regards
Andre
1 2 3
Next ›   Last »