1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
| ; CGI using native PB
;---------------------------------
; Tested on Windows with SAMBAR server
; Tested on linux hosting with apache
;---------------------------------
; This code is inspired and big parts of it using rings code here:
; http://www.purebasic.fr/english/viewtopic.php?p=174680#174680
;
; Also it seems the strings in PB can't include NULL chars so i used pdwyer inMem function from here:
; http://www.purebasic.fr/english/viewtopic.php?p=201392#201392
;
; The diffrence between this code and Rings is that:
; - GET was not supported - Now it is
; - POST allowed only 1 file upload - Now unlimited files
; - POST with only 1 file did not allowed variables - Now unlimited variables + files in the same POST
;
; I did the code to myself since it can be hosted in linux hosting, but i decided to post it in the forums
; Maybe someone could make use of it and maybe improve it
;
; By no means i am held responsible for any server/client data loss etc, use it on your own risk(!)
;
; Any suggestions and comments are welcome tough :D
;---------------------------------------------------------------------------------------------------------
;{ Header Variables
Structure CGI_Var
Name.s
Value.s
Filename.s ; wont be needed unless multipart
CType.s ; same as above
EndStructure
Global NewList FormVars.CGI_Var()
#TEXTPLAIN = 1
#MULTIPART = 2
#POST = 1
#GET = 2
Global *Buffer,ContentLength,DATA_TYPE,DATA_METHOD,MP_BOUNDARY.s,EOL$,DBLEOL$
EOL$ = Chr(13)+Chr(10) ;/ Note #CRLF$ = EOL$ , i did that because of pointer memory search
DBLEOL$ = EOL$+EOL$
;/ This is the top header before we post back to the browser, for example if you want to send out
;/ a jpeg file use
;/ OutputContentType.s="Content-type: image/jpeg" + #CRLF$ + CRLF$ + JPEGFILE$
OutputContentType.s="Content-type: text/html"
OutputContentType.s="Content-type: text/html;charset=UTF-8"
AnswerTitle.s="Page Title"
;/ If you are using linux i suggest to use a "../tempfolder/"
;Folder.s="../upload/"; "C:\" or "../upload/" ;
Folder.s = "" ; this will save the file where the cgi-bin directory runs
DebugInput=1
;}
;{ // Enviroment Variables
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.l InMem(StartPos.l, *MainMem, MainLen.l, *FindMem, FindLen.l)
If StartPos < 1 : StartPos = 1 : EndIf
FoundPos.l = 0
For MainArrayLoop.l = StartPos - 1 To MainLen -1
If MainArrayLoop + FindLen = MainLen
;End reached
Break
EndIf
If CompareMemory(*MainMem + MainArrayLoop, *FindMem, FindLen) = 1
FoundPos = MainArrayLoop + 1
Break
EndIf
Next
ProcedureReturn FoundPos
EndProcedure
Procedure SaveFileValue(BufferAndLen$,FullFileName$)
FF=CreateFile(#PB_Any,FullFileName$)
If FF
a = Val(StringField(BufferAndLen$,1,"|"))
b = Val(StringField(BufferAndLen$,2,"|"))
WriteData(FF,*Buffer+a,b)
CloseFile(FF)
EndIf
EndProcedure
Procedure Debug_Data(folder$)
FF=CreateFile(#PB_Any,folder$+Str(Date())+"-test.hex") ;/ make the save log dynamic by date
If FF
WriteData(FF,*Buffer,ContentLength)
CloseFile(FF)
EndIf
EndProcedure
Procedure Get_Form_Vars()
If DATA_METHOD = #POST
If DATA_TYPE = #TEXTPLAIN ; it means it is only text with no file attachments simple lines in short
BufferData$ = PeekS(*Buffer,ContentLength,#PB_UTF8)
i = 0
Repeat
i+1
Argument.s=Trim(StringField(BufferData$,i, #CRLF$))
If Len(Argument)>0 And Argument<>#CRLF$
AddElement(FormVars())
FormVars()\Name = StringField(Argument,1,"=")
FormVars()\Value = StringField(Argument,2,"=")
EndIf
Until Argument.s=""
ElseIf DATA_TYPE = #MULTIPART ; content disposition is found = FILE(S) + data to be parsed
DoneMP.l=0
a.l=1
;/ all these vars are for memory search to cut and paste in our linked list
MP_B_EOL.s = MP_BOUNDARY + EOL$
BLen=Len(MP_BOUNDARY)
B_EOL_Len=Len(MP_B_EOL)
length=ContentLength
Repeat
a = InMem(a,*Buffer,length,@MP_B_EOL,B_EOL_Len) ;/ Searching for Boundary+EOL first time
If a
b = InMem(a+B_EOL_Len,*Buffer,length,@EOL$,2) ;/ Now getting content disposition part info
PartInfo$ = PeekS(*Buffer+a+B_EOL_Len-1,b-(a+B_EOL_Len))
If FindString(LCase(PartInfo$),"filename=",1) ; oh its a file ;)
AddElement(FormVars())
; This is done in a loop to get values from impropar browsers
; who gives non valid syntax by values order
j = 2 ; starting from 2 since 1 is content disposition
Repeat
Tempa$ = Trim(StringField(PartInfo$,j,";"))
If Len(Tempa$) > 0
Tempb$ = StringField(Tempa$,1,"=")
Tempc$ = StringField(Tempa$,2,"=")
If LCase(Tempb$)="name"
FormVars()\Name = RemoveString(Tempc$,Chr(34)) ; removing the ""
ElseIf LCase(Tempb$)="filename"
FormVars()\Filename = RemoveString(Tempc$,Chr(34)) ; same as above + gives full path name!
EndIf
EndIf
j+1
Until Tempa$ = ""
; Now content type
c = InMem(b+1,*Buffer,length,@DBLEOL$,4)
FormVars()\CType = Trim( StringField( PeekS(*Buffer+b+1,c-(b+1)) ,2,":"))
; Now get the file content
d = InMem(c+4,*Buffer,length,@MP_BOUNDARY,BLen)
FileLen.l=d-(c+5+1)
FormVars()\Value = Str(c+3)+"|"+Str(FileLen) ;/ save this in [*Buffer+startpos|length] using SaveFileValue()
a = d ; to the next file/value
Else ; its a normal variable to process
AddElement(FormVars())
Tempa$ = (StringField(PartInfo$,2,";"))
FormVars()\Name = RemoveString(StringField(Tempa$,2,"="),Chr(34))
c = InMem(b+3,*Buffer,length,@EOL$,2)
FormVars()\Value = PeekS(*Buffer+b+3,c-(b+3))
a = c
EndIf
Else ; could not found boundary to our next element so we are out of here
DoneMP=1
EndIf
Until DoneMP=1
EndIf
ElseIf DATA_METHOD = #GET
; No need to check here the DATA_TYPE since get can only get text and no binary data!
i = 0
Repeat
i+1
Argument.s=Trim(StringField(CGI_QUERY_STRING,i, "&"))
If Len(Argument)>0
AddElement(FormVars())
FormVars()\Name = StringField(Argument,1,"=")
FormVars()\Value = StringField(Argument,2,"=")
EndIf
Until Argument.s=""
; Thats it! we got all data - BUT a special chars like =&? and more needed to be parsed in the value field
;// web escape encoding needed here to be done.
Else
; Error! exit with error
EndIf
EndProcedure
Procedure Get_CGI_ENV_Vars() ; // Init Enviroment Variables
; * 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
Procedure Init_CGI_Data() ;// Must come first
OpenConsole() ;Must come before Any console stuff
EnableGraphicalConsole(0) ;set for redirecting also text mode only
Get_CGI_ENV_Vars(); init Env Vars
ContentLength = Val(CGI_CONTENT_LENGTH) ;length of read data
*Buffer = AllocateMemory(ContentLength) ;We read the buffer now
Result= ReadConsoleData(*Buffer, ContentLength)
If LCase(CGI_CONTENT_TYPE)="text/plain"
DATA_TYPE = #TEXTPLAIN
ElseIf Left(LCase(CGI_CONTENT_TYPE),Len("multipart/form-data"))="multipart/form-data"
DATA_TYPE = #MULTIPART ;// In multipart we have BOUNDARY signatures to seperate each file/value
MP_BOUNDARY="--"+StringField(CGI_CONTENT_TYPE,2,"=") ; adding -- to start of signature
EndIf
If LCase(CGI_REQUEST_METHOD)="post" : DATA_METHOD = #POST : EndIf
If LCase(CGI_REQUEST_METHOD)="get" : DATA_METHOD = #GET : EndIf
Get_Form_Vars()
EndProcedure
;//----------------------
; Start the program here
Init_CGI_Data()
;//----------------------
;Debug Content If flagged
If DebugInput : Debug_Data(Folder) : EndIf
Info$ =""
ForEach FormVars()
objline$ = FormVars()\Name
If FormVars()\Filename = ""
objline$ + " ["+FormVars()\Value+"]<br>"
Else
; its a file so lets save it
fn$ = Folder + GetFilePart(FormVars()\Filename)
SaveFileValue(FormVars()\Value,fn$)
objline$ + " [file '" + fn$ + "' saved]<br>"
EndIf
Info$ + objline$
Next
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>"
FreeMemory(*Buffer);Free Buffer
Written=WriteConsoleData(@HttpAnswer$, Len(HttpAnswer$)) ;write Data To console pipe |
Partager