my own samples for using cgi with purebasic and Apache (mostly used webserver) from the xamp-package.
Thx freak for the hint with the GetEnvironment...() .
(see http://www.purebasic.fr/english/viewtopic.php?t=25030 )
Compile the Purecode to a Console-Exe named 'CGIMIMAGE.exe"
and put it in the cgi-bin folder of xamp .
The html-example put in the doc's folder of xamp (normaly 'htdocs')
Start your brwoser and navigate to 127.0.0.1 .......
Fee free to use this code in your app !
Its Pure Source, no Lib no version problems !
@Andre: you can include this in the purearea-snippets
This is a simple html-page:
Code: Select all
<H3>MultiImage Request Form cgitest</H3>
<P>
<FORM ACTION="../cgi-bin/CGIMIMAGE.exe" METHOD="POST" ENCTYPE="multipart/form-data">
<INPUT TYPE=FILE NAME="P1_imagefile" SIZE=35 MAXLENGTH=50><BR>
</P>
<P>
<INPUT TYPE="submit" VALUE="Send the pictures">
</P>
</FORM>
<FORM name="textsending_easy" ACTION="../cgi-bin/CGIMIMAGE.exe" ENCTYPE="text/plain" METHOD="POST">
<INPUT TYPE=Text Name="P2_MyText" Value="Type some stuff to send here" SIZE=35 MAXLENGTH=50></P>
<P>
<INPUT TYPE="submit" VALUE="Send the Text">
</P>
</FORM>
<form name="Layoutbereich1FORM" action="../cgi-bin/CGIMIMAGE.exe"" enctype="text/plain" method="post">
using Purebasic<input id="Formularmarkierungsfeld1" name="Purebasic" value="yes" type="checkbox"><br>
using VB<input id="Formularmarkierungsfeld2" name="Visual Basic" value="holla" type="checkbox"><br>
using DotNet<input id="Formularmarkierungsfeld3" name="VB.NET" value="uups" type="checkbox"><br>
Use other stuff<input id="Formularmarkierungsfeld4" name="Other" value="ok" type="checkbox"><br>
your name<input id="Formulareditierfeld5" name="NickName" value="RINGS" size="40" maxlength="40" type="text"><br>
your email<input id="Formulareditierfeld6" name="email-addi" value="karl@nospam.fr" size="43" maxlength="43" type="text"><br>
<input name="FormularHandler1" value="Sending" id="FormularHandler1" type="submit"></td>
</form>
Code: Select all
;CGI-Example for Web-server using
; native Purebasic, tested with apache
; (C) 2006 by Siegfried Rings
; free for use,
; but forbidden to making shitty non-source librarys from !
; and also forbidden to make shitty source-collections from (Except my permission)
; and also PLZ do Not Remove this shitty Header ;) !!!!
;Compile as Console-Exe
;ToDo: A Lot, the filsize is not exactly
; Test with other servers
OutputContentType.s="Content-type: text/html"
OutputContentType.s="Content-type: text/html;charset=UTF-8"
AnswerTitle.s="Answer from PureBasic-CGI"
Folder.s=""; "C:\"
DebugInput=0
Global CGI_AUTH_TYPE.s
Global CGI_CONTENT_LENGTH.s
Global CGI_CONTENT_TYPE.s
Global CGI_DOCUMENT_ROOT.s
Global CGI_GATEWAY_INTERFACE.s
Global CGI_PATH_INFO.s
Global CGI_PATH_TRANSLATED.s
Global CGI_QUERY_STRING.s
Global CGI_REMOTE_ADDR.s
Global CGI_REMOTE_HOST.s
Global CGI_REMOTE_IDENT.s
Global CGI_REMOTE_PORT.s
Global CGI_REMOTE_USER.s
Global CGI_REQUEST_URI.s
Global CGI_REQUEST_METHOD.s
Global CGI_SCRIPT_NAME.s
Global CGI_SCRIPT_FILENAME.s
Global CGI_SERVER_ADMIN.s
Global CGI_SERVER_NAME.s
Global CGI_SERVER_PORT.s
Global CGI_SERVER_PROTOCOL.s
Global CGI_SERVER_SIGNATURE.s
Global CGI_SERVER_SOFTWARE.s
Global CGI_HTTP_ACCEPT.s
Global CGI_HTTP_ACCEPT_ENCODING.s
Global CGI_HTTP_ACCEPT_LANGUAGE.s
Global CGI_HTTP_COOKIE.s
Global CGI_HTTP_FORWARDED.s
Global CGI_HTTP_HOST.s
Global CGI_HTTP_PRAGMA.s
Global CGI_HTTP_REFERER.s
Global CGI_HTTP_USER_AGENT.s
Procedure Get_CGI_Var()
; * AUTH_TYPE - Describes the authentication method used by the web browser If any authentication method was used. This is Not set unless the script is Protected.
CGI_AUTH_TYPE = GetEnvironmentVariable("AUTH_TYPE")
; * CONTENT_LENGTH - This is used For scripts that are receiving form Data using the POST method. This variable tells the byte length of the CGI input Data stream. This is required To Read the Data from the standard input With the POST method.
CGI_CONTENT_LENGTH = GetEnvironmentVariable("CONTENT_LENGTH")
; * CONTENT_TYPE - Tells the media type of Data being received from the user. this is used For scripts called using the POST method.
CGI_CONTENT_TYPE = GetEnvironmentVariable("CONTENT_TYPE")
; * DOCUMENT_ROOT - The root path To the home HTML page For the server. Example:
; /home/httpd/html
CGI_DOCUMENT_ROOT = GetEnvironmentVariable("DOCUMENT_ROOT")
; * GATEWAY_INTERFACE - The version of the common gateway Interface (CGI) specification being used To exchange the Data between the client And server. this is normally CGI/1.1 For the current revision level of 1.1.
; Example: CGI/1.1
CGI_GATEWAY_INTERFACE = GetEnvironmentVariable("GATEWAY_INTERFACE")
; * PATH_INFO - Extra path information added To the End of the URL that accessed the server side script program.
CGI_PATH_INFO = GetEnvironmentVariable("PATH_INFO")
; * PATH_TRANSLATED - A translated version of the PATH_INFO variable translated by the webserver from virtual To physical path information.
CGI_PATH_TRANSLATED = GetEnvironmentVariable("PATH_TRANSLATED")
; * QUERY_STRING - This string contains any information at the End of the server side script path that followed a question mark. Used To Return Data If the GET method was used by a form. There are length restrictions To the QUERY_STRING. Example of how To set it in HTML:
; <A HREF="/cgi-bin/hits.pl?mainpage></A>
; The information after the ? is the QUERY_STRING which is "mainpage" in this Case. How it looks on the server side
; mainpage
CGI_QUERY_STRING = GetEnvironmentVariable("QUERY_STRING")
; * REMOTE_ADDR - The IP address of the client computer. Example:
; 132.15.28.124
CGI_REMOTE_ADDR = GetEnvironmentVariable("REMOTE_ADDR")
; * REMOTE_HOST - The fully qualified domain name of the client machine making the HTTP request. It may Not be possible To determine this name since many client computers names are Not recorded in the DNS system. Example:
; comp11.mycompanyproxy.com
CGI_REMOTE_HOST = GetEnvironmentVariable("REMOTE_HOST")
; * REMOTE_IDENT - The ability To use this variable is limited To servers that support RFC 931. This variable may contain the client machine's username, but it is intended to be used for logging purposes only, when it is available.
CGI_REMOTE_IDENT = GetEnvironmentVariable("REMOTE_IDENT")
; * REMOTE_PORT - The clients requesting port. An example:
; 3465
CGI_REMOTE_PORT = GetEnvironmentVariable("REMOTE_PORT")
; * REMOTE_USER - If the CGI script was Protected And the user had To be logged in To get access To the script, this value will contain the user's log in name
CGI_REMOTE_USER = GetEnvironmentVariable("REMOTE_USER")
; * REQUEST_URI - The path To the requested file by the client. An example:
; /cgi-bin/join.pl?button=on
CGI_REQUEST_URI = GetEnvironmentVariable("REQUEST_URI")
; * REQUEST_METHOD - This describes the request method used by the browser which is normally GET, POST, Or HEAD.
CGI_REQUEST_METHOD = GetEnvironmentVariable("REQUEST_METHOD")
; * SCRIPT_NAME - The virtual path of the CGI script being executed. Example:
; /cgi-bin/join.pl
CGI_SCRIPT_NAME = GetEnvironmentVariable("SCRIPT_NAME")
; * SCRIPT_FILENAME - Example:
; /home/httpd/cgi-bin/join.pl
CGI_SCRIPT_FILENAME = GetEnvironmentVariable("SCRIPT_FILENAME")
; * SERVER_ADMIN - The e-mail address of the server administrator. Example:
; webadmin@myhost.mycompany.org
CGI_SERVER_ADMIN = GetEnvironmentVariable("SERVER_ADMIN")
; * SERVER_NAME - The server hostname, IP address Or DNS alias name shown As a self referencing URL. This does Not include the protocol identifier such As "HTTP:", the machine name, Or port number. Example:
; myhost
CGI_SERVER_NAME = GetEnvironmentVariable("SERVER_NAME")
; * SERVER_PORT - The port number the HTTP requests And responses are being sent on. Example:
; 80
CGI_SERVER_PORT = GetEnvironmentVariable("SERVER_PORT")
; * SERVER_PROTOCOL - This value is normally HTTP which describes the protocol being used between the client And server computers. Example:
; HTTP/1.1
CGI_SERVER_PROTOCOL = GetEnvironmentVariable("SERVER_PROTOCOL")
; * SERVER_SIGNATURE - Server information specifying the name And version of the web server And the port being serviced. Example:
; Apache/1.3.12 Server at mdct-dev3 Port 80
CGI_SERVER_SIGNATURE = GetEnvironmentVariable("SERVER_SIGNATURE")
; * SERVER_SOFTWARE - The name And version of the web server. Example:
; Apache/1.3.12 (Unix) (Red Hat/Linux) PHP/3.0.15 mod_perl/1.21
CGI_SERVER_SOFTWARE = GetEnvironmentVariable("SERVER_SOFTWARE")
; * HTTP_ACCEPT - The media types of Data that the client browser can accept. these Data types are separated by commas. An example:
; image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*
CGI_HTTP_ACCEPT = GetEnvironmentVariable("HTTP_ACCEPT")
; * HTTP_ACCEPT_ENCODING An example:
; gzip, deflate
CGI_HTTP_ACCEPT_ENCODING = GetEnvironmentVariable("HTTP_ACCEPT_ENCODING")
; * HTTP_ACCEPT_LANGUAGE - The language the client browser accepts. Example:
; en-us
CGI_HTTP_ACCEPT_LANGUAGE = GetEnvironmentVariable("HTTP_ACCEPT_LANGUAGE")
; * HTTP_COOKIE - Used As an environment variable that contains cookies associated With the server domain from the browser.
CGI_HTTP_COOKIE = GetEnvironmentVariable("HTTP_COOKIE")
; * HTTP_FORWARDED An example:
; by http://proxy-nt1.yourcompany.org:8080 (Netscape-Proxy/3.5)
CGI_HTTP_FORWARDED = GetEnvironmentVariable("HTTP_FORWARDED")
; * HTTP_HOST An example:
; yourwebhost.yourcompany.org
CGI_HTTP_HOST = GetEnvironmentVariable("HTTP_HOST")
; * HTTP_PRAGMA - An example:
; No-Cache
CGI_HTTP_PRAGMA = GetEnvironmentVariable("HTTP_PRAGMA")
; * HTTP_REFERER - The page address where the HTTP request originated. An example:
; http://ctdp.tripod.com/independent/web/cgi/cgimanual/index.html
CGI_HTTP_REFERER = GetEnvironmentVariable("HTTP_REFERER")
; * HTTP_USER_AGENT - The name of the client web browser being used To make the request. Example:
; Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)
CGI_HTTP_USER_AGENT = GetEnvironmentVariable("HTTP_USER_AGENT")
EndProcedure
OpenConsole() ;Needed For Output
EnableGraphicalConsole(0) ;set for redirecting
;Letz do some basic CGI-Stuff, get all those environment variables
Get_CGI_Var()
ContentLength = Val(CGI_CONTENT_LENGTH) ;length of post
;We read the buffer now
*Buffer = AllocateMemory(ContentLength)
Result= ReadConsoleData(*Buffer, ContentLength)
;Debug content
If DebugInput
FF=CreateFile(#PB_Any,"test.hex")
If FF
WriteData(FF,*Buffer,ContentLength)
CloseFile(FF)
EndIf
EndIf
Info$ =""
NewList Arg.s()
If LCase(CGI_CONTENT_TYPE)="text/plain"
InputBuffer$ = PeekS(*Buffer)
;MessageRequester("Info ",InputBuffer$,0)
l=0
Repeat
l+1
Argument.s=StringField(InputBuffer$,l, #CRLF$)
AddElement(Arg())
Arg()=LTrim(Argument)
Until Argument.s=""
ForEach Arg()
Info$ + Arg() + "<br>" ;Only for Debugging
Next
Else
If Left(LCase(CGI_CONTENT_TYPE),Len("multipart/form-data"))="multipart/form-data"
InputBuffer$ = PeekS(*Buffer);we can check that in a string, not secure, but works
;MessageRequester("Info ",InputBuffer$,0)
Info$=InputBuffer$
;search For known patterns
Res=FindString(InputBuffer$,"Content-Disposition:",1)
If Res
Res2=FindString(InputBuffer$,Chr(10),Res)
If Res2
ContentDisposition.s=Mid(InputBuffer$,Res,Res2-Res-1)
ContentDisposition.s=Right(ContentDisposition.s,Len(ContentDisposition.s)-Len("Content-Disposition:"));shorten buffer
l=0
Repeat
l+1
Argument.s=StringField(ContentDisposition.s,l, ";")
AddElement(Arg())
Arg()=LTrim(Argument)
Until Argument.s=""
EndIf
EndIf
Res=FindString(InputBuffer$,"Content-Type:",1)
If Res
Res2=FindString(InputBuffer$,Chr(10),Res)
If Res2
ContentType.s=Mid(InputBuffer$,Res,Res2-Res-1)
ContentType.s=Right(ContentType.s,Len(ContentType.s)-Len("Content-Type:"));shorten buffer
Argument.s=StringField(ContentType.s,1, ";")
AddElement(Arg())
Arg()="Content-Type="+Argument
EndIf
EndIf
ForEach Arg();iterate all arguments
Info$ + Arg() + "<br>" ;Only for Debugging
;MessageRequester("Info ","#" + arg()+"#",0)
If Left(Arg(),9) = "filename="; oh , its a file to save
;MessageRequester("Info ","Saving File " + arg(),0)
I=0
I=FindString(InputBuffer$,Chr(13),I+1)
I=FindString(InputBuffer$,Chr(13),I+1)
I=FindString(InputBuffer$,Chr(13),I+1)
I=FindString(InputBuffer$,Chr(13),I+1)
If I>0
Filename.s=Right(arg(),Len(Arg())-9)
If Left(Filename.s,1)=Chr(34) :Filename.s=Right(Filename.s,Len(Filename.s)-1): EndIf
If Right(Filename.s,1)=Chr(34) :Filename.s=Left(Filename.s,Len(Filename.s)-1): EndIf
Filename=GetFilePart(Filename)
Filename.s = Folder + Filename.s
FF=CreateFile(#PB_Any,Filename)
If FF
WriteData(FF,*Buffer+I+1,ContentLength-I)
CloseFile(FF)
info$ + " File saved under " + Filename + "<br>"
Else
info$ + " cannot create File under " + Filename + "<br>"
EndIf
EndIf
EndIf
Next
Else
;what else a type ?
;If = "application/octetstream"
EndIf
EndIf
FreeMemory(*Buffer);Free Buffer
HttpAnswer$ = OutputContentType.s + #CRLF$ + #CRLF$
HttpAnswer$ + "<html>"
HttpAnswer$ + "<header>"
HttpAnswer$ + "<title>" + AnswerTitle.s + "</title>"
HttpAnswer$ + "</header>"
HttpAnswer$ + "<body>"
HttpAnswer$ + "Hi, received your Data:<br>"
;we wanna show the cgi_Vars
Info$ + "<br>"
Info$ + "CGI_AUTH_TYPE=" + CGI_AUTH_TYPE + "<br>"
Info$ + "CGI_CONTENT_LENGTH=" + CGI_CONTENT_LENGTH + "<br>"
Info$ + "CGI_CONTENT_TYPE=" + CGI_CONTENT_TYPE + "<br>"
Info$ + "CGI_DOCUMENT_ROOT=" + CGI_DOCUMENT_ROOT + "<br>"
Info$ + "CGI_GATEWAY_INTERFACE=" + CGI_GATEWAY_INTERFACE + "<br>"
Info$ + "CGI_PATH_INFO=" + CGI_PATH_INFO + "<br>"
Info$ + "CGI_PATH_TRANSLATED=" + CGI_PATH_TRANSLATED + "<br>"
Info$ + "CGI_QUERY_STRING=" + CGI_QUERY_STRING + "<br>"
Info$ + "CGI_REMOTE_ADDR=" + CGI_REMOTE_ADDR + "<br>"
Info$ + "CGI_REMOTE_HOST=" + CGI_REMOTE_HOST + "<br>"
Info$ + "CGI_REMOTE_IDENT=" + CGI_REMOTE_IDENT + "<br>"
Info$ + "CGI_CGI_REMOTE_PORT=" + CGI_REMOTE_PORT + "<br>"
Info$ + "CGI_REMOTE_USER=" + CGI_REMOTE_USER + "<br>"
Info$ + "CGI_REQUEST_URI=" + CGI_REQUEST_URI + "<br>"
Info$ + "CGI_REQUEST_METHOD=" + CGI_REQUEST_METHOD + "<br>"
Info$ + "CGI_SCRIPT_NAME=" + CGI_SCRIPT_NAME + "<br>"
Info$ + "CGI_SCRIPT_FILENAME=" + CGI_SCRIPT_FILENAME + "<br>"
Info$ + "CGI_SERVER_ADMIN=" + CGI_SERVER_ADMIN + "<br>"
Info$ + "CGI_SERVER_NAME=" + CGI_SERVER_NAME + "<br>"
Info$ + "CGI_SERVER_PORT=" + CGI_SERVER_PORT + "<br>"
Info$ + "CGI_SERVER_PROTOCOL=" + CGI_SERVER_PROTOCOL + "<br>"
Info$ + "CGI_SERVER_SIGNATURE=" + CGI_SERVER_SIGNATURE + "<br>"
Info$ + "CGI_SERVER_SOFTWARE=" + CGI_SERVER_SOFTWARE + "<br>"
Info$ + "CGI_HTTP_ACCEPT=" + CGI_HTTP_ACCEPT + "<br>"
Info$ + "CGI_HTTP_ACCEPT_ENCODING=" + CGI_HTTP_ACCEPT_ENCODING + "<br>"
Info$ + "CGI_HTTP_ACCEPT_LANGUAGE=" + CGI_HTTP_ACCEPT_LANGUAGE + "<br>"
Info$ + "CGI_HTTP_COOKIE=" + CGI_HTTP_COOKIE + "<br>"
Info$ + "CGI_HTTP_FORWARDED=" + CGI_HTTP_FORWARDED + "<br>"
Info$ + "CGI_HTTP_HOST=" + CGI_HTTP_HOST + "<br>"
Info$ + "CGI_HTTP_PRAGMA=" + CGI_HTTP_PRAGMA + "<br>"
Info$ + "CGI_HTTP_REFERER=" + CGI_HTTP_REFERER + "<br>"
Info$ + "CGI_HTTP_USER_AGENT=" + CGI_HTTP_USER_AGENT + "<br>"
HttpAnswer$ + Info$
HttpAnswer$ + "</body>"
HttpAnswer$ + "</html>"
Written=WriteConsoleData(@HttpAnswer$, Len(HttpAnswer$)) ;write Data To console pipe
; IDE Options = PureBasic v4.02 (Windows - x86)
; ExecutableFormat = Console
; CursorPosition = 4
; Folding = -
; Executable = C:\xampp\cgi-bin\CGIMIMAGE.exe
; DisableDebugger