www.digitalmars.com         C & C++   DMDScript  

D - CGI module

reply "Mårten Ask" <majbritt37 hotmail.com> writes:
Hello,

I've written a very basic cgi module. It parses headers, querystrings and
forms as well as getting and settings cookies. It needs lots of more work,
but is quite useful already. Anyway, I've tested it a little and was hoping
that others might test it aswell so I can add useful stuff. I've only used
PHP before and all this strong typing really hurts my brain! :-) So I could
use some help..

Anyway, I'll send the module along and here's a small test program I wrote:

import cgi;

void main()
{
 Request req = new Request();
 Response res = new Response();

 if(req.serverVariable("REQUEST_METHOD") == "POST")
 {
  char[] name = req.formString("name"); // returns formfield value as string
  char[] comment = req.formString("comment");
  res.cookie("name", name, "", "", "", 0);
  res.write("<html>\n<head>\n<title>CGI-test</title>\n</head>\n");
  res.write("<body>\n");
  res.write("Hello <b>"~name~"</b><p>\n");
  res.write("You wrote:\n");
  res.write("<blockquote><b>\""~comment~"\"</b></blockquote>\n");
  res.write("<a href='cgitest.exe'>Back to form</a>");
  res.write("</body></html>");
  res.flush();
 }
 else
 {
  char[] cookie;
  try
  {
   cookie = req.cookie("name");
  }
  catch(CGIError) // cookie wasn't set!
  {
   cookie = "";
  }

  printf(res.getHeaders()~"\n\n");
  printf("<html>\n<head>\n<title>CGI-test</title>\n</head>\n");
  printf("<body>\n");
  printf("<form method='post' action='cgitest.exe'>\n");
  printf("Name: <input type='text' name='name' value='%.*s'><br>\n",
cookie);
  printf("Comment: <input type='text' name='comment'><br>\n");
  printf("<input type='submit' value='Send!'>\n");
  printf("</form></body></html");
 }
}


begin 666 cgi.d





M;VXG="!H879E('1O(&-O;G9E<G0 <W1R:6YG<R!A<R!M=6-H+ T*<')I=F%T

M;U-T<FEN9RAG971E;G8H=&]3=')I;F=Z*&ME>2DI*3L-"GT-" T*+RH <&%R
M<V5S(&9O<FT ;W( <75E<GES=')I;F< 9&%T82 J+PT*<')I=F%T92!C;&%S

M<&%R<V5S(&9O<FT ;W( =7)L(&1A=&$ =&\ 86X 87-S;V-I871I=F4 87)R

M>PD-" D)8VAA<EM=6UT 9C$L(&8R.PT*"0EV87)S(#T <W1R:6YG+G)E<&QA


M" D)>PT*"0D)9C( /2!S<&QI="AF,5MI72P (CTB*3L-" D)"6EF*&9I96QD
M<UMF,ELP75TN;&5N9W1H(#X ,"D +R\ :V5Y(&%L9')E861Y(&5X:7-T<PT*
M"0D)>PT*"0D)"6-H87);72!K97D /2!F:65L9'-;9C);,%U=.PT*"0D)"6ME
M>2!^/2 B.R( ?B!F,ELQ73L-" D)"0D-" D)"0EF:65L9'-;9C);,%U=(#T 
M:V5Y.PT*"0D)?0T*"0D)96QS92 -" D)"7L-" D)"0EF:65L9'-;9C);,%U=
M(#T
M<R!T:&4 =F%L=64 ;V8 82!F;W)M(&9I96QD+B!R971U<FYS(&5M<'1Y('-T
M<FEN9R!O;B!F86EL=7)E+ T*"2 J(&EF(&UO<F4 =&AA;B!O;F4 9FEE;&0 
M:&%S('1H92!S86UE(&YA;64 :70 ;VYL>2!R971U<FYS('1H92!L87-T( T*
M"2 J(&9O=6YD+B -" D *B\-" EC:&%R6UT 9V5T4W1R:6YG*&-H87);72!K


M1TE%<G)O<B B1FEE;&0 9&]E<VXG="!E>&ES=#H


M;F4 9FEE;&0 :&%S('1H92!S86UE(&YA;64 :70 ;VYL>2!R971U<FYS('1H



M:')O=R!N97< 0T=)17)R;W(H:V5Y('X (B!I<R!N;W0 86X :6YT(2(I.R -
M" D)?0T*"7T-" D-" DO*B!R971U<FYS(&9O<FUF:65L9"!V86QU97, =VET
M:"!S86UE(&YA;65S(&%S(&%N(&%R<F%Y(&]F('-T<FEN9W,N( T*"2 J(&=O
M;V0 9F]R(&UU;'1I<&QE('-E;&5C="!B;WAE<RX *B\-" EC:&%R6UU;72!G
M971!<G)A>2AC:&%R6UT :V5Y*0T*"7L-" D)8VAA<EM=6UT 9&EC="P <F5S
M=6QT.PT*"0ED:6-T(#T



M=R!087)S97(H*3L-" EP+G!A<G-E*")N/4TE831R=&5N*T%S:R9G/3,T)FAE

M(BD /3T (DWE<G1E;B!!<VLB*3L-" EA<W-E<G0H<"YG971);G0H(F<B*2 ]

M;R(I.PT*"6%S<V5R="AP+F=E=$%R<F%Y*")H96HB*5LQ72 ]/2 B:&5L;&\R

M('5S92!I="!D:7)E8W1L>2!O<B!V:6$ =&AE(%)E<75E<W0 ;W( 4F5S<&]N
M<V4 8VQA<W-E<R J+PT*8VQA<W, 1F]R;2 Z(%!A<G-E< T*>PT*"71H:7,H
M*0T*"7L-" D):68H*&5N=B B4D5154535%]-151(3T0B*2 ]/2 B4$]35"(I


M" T*+RH <75E<GES=')I;F< <&%R<V5R+B!U<V4 :70 9&ER96-T;'D ;W( 

M<R!1=65R>7-T<FEN9R Z(%!A<G-E< T*>PT*"71H:7,H*0T*"7L-" D)8VAA
M<EM=('%S(#T 96YV*")1545265]35%))3D<B*3L-" D):68H<7,N;&5N9W1H
M(#X
M971S(&%N9"!S971S(&-O;VMI97,N('5S92!I="!D:7)E8W1L>2!O<B!V:6$ 
M=&AE(%)E<75E<W0 ;W( 4F5S<&]N<V4 8VQA<W-E<R J+PT*8VQA<W, 0V]O

M8V]O:VEE<U1O4V5N9#L-" D-" DO*B!F971C:&5S(&-O;VMI97, *B\-" ET




M:&4 =F%L=64 ;V8 8V]O:VEE+B!T:')O=W, 0T=)17)R;W( ;VX 9F%I;'5R
M92X *B\-" EC:&%R6UT 9V5T0V]O:VEE*&-H87);72!N86UE*0T*"7L-" D)


M:6YT(&D[(&D /"!C;V]K:65S+FQE;F=T:#L

M:71T961;,%U=(#T



M('1O(&AE861E<G, *B\-" EV;VED('-E=$-O;VMI92AC:&%R6UT ;F%M92P 
M8VAA<EM=('9A;'5E+"!C:&%R6UT 97AP:7)E<RP 8VAA<EM=('!A=& L(&-H
M87);72!D;VUA:6XL(&)I="!S96-U<F4I( T*"7L-" D)8VAA<EM=(')E<W5L
M=#L-





M;'0 ?CT (CL <&%T:#TB



M<W5L=#L-
M>2!O9B!S=')I;F=S('=I=&  86QL(&-O;VMI97, 9F]U;F0 *B\-" EC:&%R

M6UU=(')E<W5L=#L-


M6W-;,%U=(#T <ULQ73L-" D)?0T*"0D-" D)<F5T=7)N(')E<W5L=#L-" E]





M($9O<FTH*3L-" D)<2 ](&YE=R!1=65R>7-T<FEN9R I.PT*"0EC(#T ;F5W
M($-O;VMI92 I.PT*"7T-" D-" DO*B!R971U<FYS('1H92!E;G9I<F]N;65N
M="!V87)I86)L92 J+PT*"6-H87);72!S97)V97)687)I86)L92AC:&%R6UT 

M='5R;G, =&AE('9A;'5E(&]F(&9O<FUF:65L9"!A<R!A('-T<FEN9R J+PT*


M=7)N<R!A(&9O<FUF:65L9"!V86QU92!A<R!A;B!I;G0 *B\-" EI;G0 9F]R
M;4EN="AC:&%R6UT 9FEE;&1.86UE*0T*"7L-" D)<F5T=7)N(&8N9V5T26YT

M9', ;F%M960 )V9I96QD;F%M92< 87, 87)R87D ;V8 <W1R:6YG<R J+PT*
M"6-H87);75M=(&9O<FU!<G)A>2AC:&%R6UT 9FEE;&1.86UE*0T*"7L-" D)

M=7)N<R!Q=65R>7-T<FEN9R!V86QU92!A<R!S=')I;F< *B\-" EC:&%R6UT 



M"7L-" D)<F5T=7)N('$N9V5T26YT*&YA;64I.PT*"7T-" D-" DO*B!R971U
M<FYS(&%L;"!Q=65R>7-T<FEN9R!V86QU97, =VET:"!T:&4 ;F%M92 G;F%M
M92< 87, 87)R87D ;V8 <W1R:6YG<R J+PT*"6-H87);75M=('%U97)Y07)R




M8V]O:VEE<R!F;W5N9"!A<R!A<W-O8VEA=&EV92!A<G)A>2!O9B!S=')I;F=S





M;F5E9&5D+B J+PT*"79O:60 8V]O:VEE*&-H87);72!N86UE+"!C:&%R6UT 
M=F%L=64L(&-H87);72!E>'!I<F5S+"!C:&%R6UT <&%T:"P 8VAA<EM=(&1O


M"2\J('=R:71E<R G<W1R)R!T;R!C=7)R96YT(&)U9F9E<B J+PT*"79O:60 

M"0T*"2\J('=R:71E<R!A(&YE=R!H96%D97( *B\-" EV;VED(&AE861E<BAC
M:&%R6UT ;F%M92P 8VAA<EM=('9A;'5E*0T*"7L-" D):&5A9&5R<R!^/2!N





M;B=T(&EN('1H97)E+"!T97AT+VAT;6P :7, 87-S=6UE9"X-" D *B!D;VXG
M="!F;W)G970 =&\ 861D('1W;R!L:6YE8G)E86MS(&)E9F]R92!A;GD 8V]N

M86QL4V5T(#T ,#L-" D)9F]R*&EN="!I.R!I(#P :&5A9&5R<RYL96YG=& [
M(&DK*RD-" D)>PT*"0D):68H:&5A9&5R<UMI75LP+BXQ,ET /3T (D-O;G1E






M>PT*"0ER971U<FX 8G5F9F5R.PT*"7T-" D-" DO*B!P<FEN=', =&AE(&AE
M861E<G, 86YD(&)U9F9E<B J+PT*"79O:60 9FQU<V H*0T*"7L-" D)8VAA
M<EM=(')E<W5L=#L-" D)<F5S=6QT(#T 9V5T2&5A9&5R<R I?B)<;EQN(CL-


`
end
Jul 27 2003
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Mårten Ask" <majbritt37 hotmail.com> wrote in message
news:bg0tqe$2mhm$1 digitaldaemon.com...
| Hello,
|
| I've written a very basic cgi module. It parses headers, querystrings and
| forms as well as getting and settings cookies. It needs lots of more work,
| but is quite useful already. Anyway, I've tested it a little and was
hoping
| that others might test it aswell so I can add useful stuff. I've only used
| PHP before and all this strong typing really hurts my brain! :-) So I
could
| use some help..
|
| Anyway, I'll send the module along and here's a small test program I
wrote:
| ...

what do I have to do to use cgi?

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.504 / Virus Database: 302 - Release Date: 2003-07-24
Jul 27 2003
parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
Yes I'd like to know also, is their instructions to setup apache with it
somewhere?

Charles

"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:bg2477$tb5$1 digitaldaemon.com...
 "Mårten Ask" <majbritt37 hotmail.com> wrote in message
 news:bg0tqe$2mhm$1 digitaldaemon.com...
 | Hello,
 |
 | I've written a very basic cgi module. It parses headers, querystrings
and
 | forms as well as getting and settings cookies. It needs lots of more
work,
 | but is quite useful already. Anyway, I've tested it a little and was
 hoping
 | that others might test it aswell so I can add useful stuff. I've only
used
 | PHP before and all this strong typing really hurts my brain! :-) So I
 could
 | use some help..
 |
 | Anyway, I'll send the module along and here's a small test program I
 wrote:
 | ...

 what do I have to do to use cgi?

 -------------------------
 Carlos Santander


 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.504 / Virus Database: 302 - Release Date: 2003-07-24
Jul 28 2003
parent reply "Mårten Ask" <majbritt37 hotmail.com> writes:
With apache you just put the .exe in the cgi-bin directory. Then reach it at
http://localhost/cgi-bin/filename.exe


"Charles Sanders" <sanders-consulting comcast.net> skrev i meddelandet
news:bg3dmn$28uh$1 digitaldaemon.com...
 Yes I'd like to know also, is their instructions to setup apache with it
 somewhere?

 Charles

 "Carlos Santander B." <carlos8294 msn.com> wrote in message
 news:bg2477$tb5$1 digitaldaemon.com...
 "Mårten Ask" <majbritt37 hotmail.com> wrote in message
 news:bg0tqe$2mhm$1 digitaldaemon.com...
 | Hello,
 |
 | I've written a very basic cgi module. It parses headers, querystrings
and
 | forms as well as getting and settings cookies. It needs lots of more
work,
 | but is quite useful already. Anyway, I've tested it a little and was
 hoping
 | that others might test it aswell so I can add useful stuff. I've only
used
 | PHP before and all this strong typing really hurts my brain! :-) So I
 could
 | use some help..
 |
 | Anyway, I'll send the module along and here's a small test program I
 wrote:
 | ...

 what do I have to do to use cgi?

 -------------------------
 Carlos Santander


 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.504 / Virus Database: 302 - Release Date: 2003-07-24
Jul 28 2003
parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Mårten Ask" <majbritt37 hotmail.com> wrote in message
news:bg3n4v$2ik1$1 digitaldaemon.com...
| With apache you just put the .exe in the cgi-bin directory. Then reach it
at
| http://localhost/cgi-bin/filename.exe
|

And if I'm trying to access it from a html page? I mean, how do I put it in
a <a href=...>?

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.504 / Virus Database: 302 - Release Date: 2003-07-24
Jul 30 2003