May 19, 2011
> Hmm...Might be problem for it to be main... Security concerned because of the publicity done my M$ and others also. When they pushed for ASP/JSP...

That's such backward nonsense I find it hard to believe anyone would seriously believe it.
May 19, 2011
"Matthew Ong" <ongbp@yahoo.com> wrote in message news:ir3anp$dgj$1@digitalmars.com...
> On 5/13/2011 4:01 AM, Nick Sabalausky wrote:
>>
>> Here's a basic "Hello world" CGI app in D:
>>
>> // hellocgi.d
>> import std.conv;
>> import std.stdio;
>>
>> void main()
> Hmm...Might be problem for it to be main... Security concerned because of the publicity done my M$ and others also. When they pushed for ASP/JSP...

What's wrong with it? This is the first I've heard of any such issue.

>> {
>>      // Read in HTTP request headers
>>      string[] requestHeaders;
>>      while(true)
>>      {
>>          string line = readln();
>>          if(line.length<= 1)
>>              break;
>>
>>          requestHeaders ~= line;
>>      }
>>
>>      // Send response headers
>>      writeln("Status: 200 OK");
>>      writeln("Content-Type: text/html; charset=UTF-8");
>>      writeln();
>>
>>      // Send content
>>      writeln("<b><i>Hello world</i></b>");
>> }
>>
>> Compile with:
>> dmd hellocgi.d
>>
>> And just stick the resulting "hellocgi.exe" (windows) or "hellocgi"
>> (other)
>> in whatever "cgi-bin"-capable directory you have on your server.
> Thanks for this Example also. I will keep it for later trial.
>

Actually, like Adam pointed out, I got the HTTP request headers part completely wrong. HTTP headers come from environment variables, and stdin is just used for any data that's been posted. So that part's totally wrong. So you'll want to just rip that part out like this:

// hellocgi.d
import std.conv;
import std.stdio;

void main()

{
     // Send response headers
     writeln("Status: 200 OK");
     writeln("Content-Type: text/html; charset=UTF-8");
     writeln();

     // Send content
     writeln("<b><i>Hello world</i></b>");
}


>> I'm sure it's possible to make an ISAPI filter or Apache module in D, but
>> I've never really done that in any langauge, and I haven't really dealt
>> with
>> DLLs much, so I wouldn't know how. But even as CGI, a web app in D is
>> likely
>> to still be much faster than one in, for instance, PHP or Ruby.
> Why I am looking for DLL as compare to exe is becuase it is a well know
> security concern
> published and accepted as minimum by the industry for MNC.
>

First I've heard of it. What's the problem?


May 23, 2011
This looks really interesting -- this will be obvious from my question -- I am a hobbyist programmer interested in D (with most experience in Python).

I can get the first cgi example (not the mixin) to compile and run on an Apache
cgi server.

I cannot get the second, cgi/mixin sample to compile. What is the appropriate dmd command line?

If I write the text verbatim and compile as a module "modulename", I get three errors, one for the function "run" indicating that "cgi" is not defined, did I mean Cgi.  The other two errors say that "modulename.run" cannot be called with argument type Cgi and that 0 arguments are expected for function type void -- and these errors indicate the line number from the arsd/cgi.d module.

I'm sure this is something simple and that I'm not yet understanding mixins and how to troubleshoot this type of error -- anyone want to point me to the nose on my face?

Thanks!

Nathan
May 23, 2011
Nathan wrote:
> I cannot get the second, cgi/mixin sample to compile. What is the appropriate dmd command line?

First, download my cgi module

http://arsdnet.net/dcode/cgi.d

Then, in the same directory write this file, hello.d:

===
import arsd.cgi;

void whatever(Cgi cgi) {
   cgi.write("Hello!");
}

mixin GenericMain!(whatever);
====


And use this command line:

dmd hello.d cgi.d


Make sure you are using a recent dmd2 compiler like 2.053 from here: http://digitalmars.com/d/2.0/changelog.html


You should get a program hello that, when run on the command line, outputs this:

=====
Cache-Control: private, no-cache="set-cookie"
Expires: 0
Pragma: no-cache
Content-Type: text/html; charset=utf-8

Hello!
=====


Copy it to a cgi enabled directory ( cgi-bin/ is preconfigured on most Apache installs) and you should be able to access it from the web:

http://arsdnet.net/cgi-bin/test



If it doesn't work, can you copy/paste the errors in here?
May 23, 2011
Also:

" other two errors say that "modulename.run" cannot be called with
argument type Cgi and that 0 arguments are expected for function type void "

When using the generic main mixin, the function you pass must always take one argument: a Cgi object.

void yourFunctionHere(Cgi cgi) { }

When the mixin is compiled, it expands to something like this:


void main() {
   auto cgi = new Cgi;
   scope(exit) cgi.close();
   try {
        yourFunctionHere(cgi);
   } catch (Exception) {
        // log and maybe display the error then exit
   }
}


So the function you pass to the mixin is called with an argument:

        yourFunctionHere(cgi);


Which might be producing the error you saw, if your function couldn't take the cgi object as it's own argument.




Why use the mixin at all? Writing main yourself might be simpler.

There's three reasons:

!) It saves you from writing the same thing over and over as your
   main function. This is a small benefit, but it's nice.

2) It can display errors to the browser in debug mode. This makes
   debugging easier. The message is right there.

3) This is the biggest one: if the main() function ever needs to
   change, your code doesn't.

Thanks to the GenericMain mixin, for example, you can switch to using FastCGI or a built in webserver simply by recompiling with a different version. None of your code has to change.
May 24, 2011
Adam,

Thanks for the step by step guide.  My problem was exactly what you outlined, that "yourFunctionHere" did not take Cgi cgi as an argument. That's all I needed!

I definitely understand the benefit of the mixin -- the overall design makes a lot of sense.

Nathan





On Mon, 23 May 2011 16:57:06 +0000 (UTC), Adam D. Ruppe
<destructionator@gmail.com> wrote:

>Also:
>
>" other two errors say that "modulename.run" cannot be called with
>argument type Cgi and that 0 arguments are expected for function type void "
>
>When using the generic main mixin, the function you pass must always take one argument: a Cgi object.
>
>void yourFunctionHere(Cgi cgi) { }
>
>When the mixin is compiled, it expands to something like this:
>
>
>void main() {
>   auto cgi = new Cgi;
>   scope(exit) cgi.close();
>   try {
>        yourFunctionHere(cgi);
>   } catch (Exception) {
>        // log and maybe display the error then exit
>   }
>}
>
>
>So the function you pass to the mixin is called with an argument:
>
>        yourFunctionHere(cgi);
>
>
>Which might be producing the error you saw, if your function couldn't take the cgi object as it's own argument.
>
>
>
>
>Why use the mixin at all? Writing main yourself might be simpler.
>
>There's three reasons:
>
>!) It saves you from writing the same thing over and over as your
>   main function. This is a small benefit, but it's nice.
>
>2) It can display errors to the browser in debug mode. This makes
>   debugging easier. The message is right there.
>
>3) This is the biggest one: if the main() function ever needs to
>   change, your code doesn't.
>
>Thanks to the GenericMain mixin, for example, you can switch to using FastCGI or a built in webserver simply by recompiling with a different version. None of your code has to change.
1 2 3 4 5 6 7 8 9 10
Next ›   Last »