Changeset 3810 for trunk/gsdl/src/w32server/httpreq.cpp
- Timestamp:
- 2003-03-09T09:08:58+13:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/src/w32server/httpreq.cpp
r3056 r3810 26 26 *********************************************************************/ 27 27 28 #include "text_t.h"29 28 #include <windows.h> 30 29 #include <stdlib.h> … … 32 31 #include <string.h> 33 32 #include <memory.h> 34 # pragma hdrstop33 #include "httpreq.h" 35 34 #include "parse.h" 36 35 #include "netio.h" 37 36 #include "settings.h" 38 #include "httpreq.h" //Had to put myself here because of the global types...39 37 #include "httpsrv.h" 40 38 #include "httpsend.h" … … 54 52 #define IO_BUFFER_SIZE 16384 //16K IO Buffer 55 53 #define MAX_HTTP_LINE_LEN 1024 //Max length of line in a header of 1024 56 #define MAX_HTTP_FIELD_NAME_LEN 128 //Max length of name field in line57 #define MAX_HTTP_FIELD_LEN 1024 //Max length of data in line58 54 59 55 //Private Function Declarations with Return Contstants … … 219 215 int GetHTTPHeaders(RequestInfoT &RequestInfo, RequestFieldsT &RequestFields) { 220 216 //Parsing and IO buffers 221 char CurLine[NETIO_MAX_LINE];222 char NextLine[NETIO_MAX_LINE];223 char FieldNameStr[MAX_HTTP_FIELD_NAME_LEN];224 char FieldValStr[MAX_HTTP_FIELD_LEN];217 text_t CurLine; 218 text_t NextLine; 219 text_t FieldNameStr; 220 text_t FieldValStr; 225 221 226 222 //Parsing and IO working vars 227 223 int ReadBufferIndex; 228 224 int DataInBuffer; 229 int Start; 230 int End; 231 int Len; 225 text_t::const_iterator next; 226 text_t::const_iterator end; 232 227 233 228 //Clear all the fields … … 245 240 RequestInfo.IOBufferSize, ReadBufferIndex, DataInBuffer, 246 241 RequestInfo.ThreadNum) != 0) return GH_ERROR; 247 if ((NextLine[0] == ' ') || (NextLine[0] == '\t')) 248 strcat(CurLine, NextLine); 249 } while ((NextLine[0] == ' ') || (NextLine[0] == '\t')); 242 if ((*(NextLine.begin()) == ' ') || (*(NextLine.begin()) == '\t')) { 243 CurLine += NextLine; 244 } 245 } while ((*(NextLine.begin()) == ' ') || (*(NextLine.begin()) == '\t')); 250 246 //Method String (first word) 251 Start = 0; 252 GetWord(RequestFields.MethodStr, CurLine, Start, End); 253 CharUpper(RequestFields.MethodStr); 247 GetWord(RequestFields.MethodStr, CurLine.begin(), CurLine.end(), next); 248 uc(RequestFields.MethodStr); 254 249 255 250 /* Added Feb 2002 - IE since about version 5 send stupid frontpage requests 256 251 for MS Document formats eg "GET /_vti_inf.html" */ 257 if ( strcmp(RequestFields.MethodStr,"OPTIONS")==0) {258 252 if (RequestFields.MethodStr == "OPTIONS") { 253 return GH_BAD_METHOD; 259 254 } 260 255 //Version String (last word) 261 GetLastWord(RequestFields.VersionStr, CurLine, Start); 262 CharUpper(RequestFields.VersionStr); 263 264 if (strncmp(RequestFields.VersionStr, "HTTP/", 5) != 0) { 256 GetLastWord(RequestFields.VersionStr, CurLine.begin(), CurLine.end(), end); 257 uc(RequestFields.VersionStr); 258 text_t::const_iterator versionbegin = RequestFields.VersionStr.begin(); 259 260 if ((RequestFields.VersionStr.size() > 5) && (substr(versionbegin, versionbegin+5) != "HTTP/")) { 265 261 //No version, assume simple request 266 262 //part after method is URI 267 for (int i = 0; i < strlen(CurLine); i++) { 268 RequestFields.URIStr.push_back(CurLine[i]); 269 } 263 RequestFields.URIStr = CurLine; 270 264 return GH_SIMPLE_REQUEST; 271 265 } … … 273 267 //URI String (in between End of first and Start of last) 274 268 //<Method> <WhiteSpace> <URI> <WhiteSpace> <Version> <CRLF> 275 // End^ Start^269 // next^ end^ 276 270 text_t spacebuffer; 277 for (int i = End; i < Start; i++) { 271 text_t::const_iterator here = next; 272 while (here != end) { 278 273 // do this to remove trailing space 279 if ( CurLine[i] == '') {280 spacebuffer.push_back( ' ');274 if (*here == ' ' || *here == '\t') { 275 spacebuffer.push_back(*here); 281 276 } else { 282 277 if (!spacebuffer.empty()) { … … 284 279 spacebuffer.clear(); 285 280 } 286 RequestFields.URIStr.push_back(CurLine[i]); 287 } 281 RequestFields.URIStr.push_back(*here); 282 } 283 here++; 288 284 } 289 285 290 286 //Only accept requests from HTTP/0.9 or HTTP/1.X clients, we'll 291 287 //assume that anything else will require an upgrade or patch 292 if ( strncmp(RequestFields.VersionStr, "HTTP/1.", 7) != 0)288 if ((RequestFields.VersionStr.size() > 7) && (substr(versionbegin, versionbegin+7) != "HTTP/1.")) { 293 289 return GH_UNKNOWN_VERSION; 290 } 294 291 295 292 //Get the rest of the lines 296 297 strcpy(CurLine, NextLine); 298 299 while (CurLine[0] != 0) {//Blank Line, we're done 293 CurLine = NextLine; 294 295 while (!CurLine.empty()) {//Blank Line, we're done 300 296 do {//Get Next Line, append it if the first charactor is space 301 297 if (GetLine(NextLine, RequestInfo.ClientSocket, RequestInfo.IOBuffer, … … 303 299 RequestInfo.ThreadNum) != 0) 304 300 return GH_ERROR; 305 if ((NextLine[0] == ' ') || (NextLine[0] == '\t')) 306 strcat(CurLine, NextLine); 307 } while ((NextLine[0] == ' ') || (NextLine[0] == '\t')); 301 if ((*(NextLine.begin()) == ' ') || (*(NextLine.begin()) == '\t')) { 302 CurLine += NextLine; 303 } 304 } while ((*(NextLine.begin()) == ' ') || (*(NextLine.begin()) == '\t')); 308 305 309 Start = 0; 310 GetWord(FieldNameStr, CurLine, Start, End); 311 CharUpper(FieldNameStr); 306 GetWord(FieldNameStr, CurLine.begin(), CurLine.end(), next); 307 uc(FieldNameStr); 312 308 313 Len = strlen(CurLine) - End; 314 memcpy(FieldValStr, CurLine + End, Len); 315 FieldValStr[Len] = 0; 309 FieldValStr = substr(next, CurLine.end()); 316 310 317 311 //Process it … … 319 313 //All constants are in canonized, thus in upper case and case sensitive 320 314 //comparisons are used 315 321 316 //--Just About Always-- 322 if (strcmp("ACCEPT:", FieldNameStr) == 0) { 323 if (RequestFields.AcceptStr[0] == '\0') { 324 strncpy(RequestFields.AcceptStr, FieldValStr, ReqAcceptStrLen - 1); 325 } 326 else { 327 //Append it with a comma 328 int AcceptStrLen = strlen(RequestFields.AcceptStr); 329 if ((ReqAcceptStrLen - AcceptStrLen) >= 10) { 330 strncat(RequestFields.AcceptStr, ", ", ReqAcceptStrLen - AcceptStrLen - 1); 331 strncat(RequestFields.AcceptStr, FieldValStr, ReqAcceptStrLen - AcceptStrLen - 3); 332 } 333 } 334 } 335 else if (strcmp("DATE:", FieldNameStr) == 0) { 336 strncpy(RequestFields.DateStr, FieldValStr, ReqDateStrLen - 1); 337 } 338 else if (strcmp("USER-AGENT:", FieldNameStr) == 0) { 339 strncpy(RequestFields.UserAgentStr, FieldValStr, ReqUserAgentStrLen - 1); 340 } 341 else if (strcmp("CONNECTION:", FieldNameStr) == 0) { 342 strncpy(RequestFields.ConnectionStr, FieldValStr, ReqConnectionStrLen - 1); 317 if (FieldNameStr == "ACCEPT:") { 318 if (!RequestFields.AcceptStr.empty()) { 319 RequestFields.AcceptStr += ", "; 320 } 321 RequestFields.AcceptStr += FieldValStr; 322 } 323 else if (FieldNameStr == "DATE:") { 324 RequestFields.DateStr = FieldValStr; 325 } 326 else if (FieldNameStr == "USER-AGENT:") { 327 RequestFields.UserAgentStr = FieldValStr; 328 } 329 else if (FieldNameStr == "CONNECTION:") { 330 RequestFields.ConnectionStr = FieldValStr; 343 331 } 344 332 //--Sometimes-- 345 else if ( strcmp("ACCEPT-LANGUAGE:", FieldNameStr) == 0) {346 strncpy(RequestFields.AcceptLangStr, FieldValStr, ReqAcceptLangStrLen - 1);347 } 348 else if ( strcmp("REFERER:", FieldNameStr) == 0) {349 strncpy(RequestFields.RefererStr, FieldValStr, ReqRefererStrLen - 1);350 } 351 else if ( strcmp("IF-MODIFIED-SINCE:", FieldNameStr) == 0) {352 strncpy(RequestFields.IfModSinceStr, FieldValStr, ReqIfModSinceStrLen - 1);333 else if (FieldNameStr == "ACCEPT-LANGUAGE:") { 334 RequestFields.AcceptLangStr = FieldValStr; 335 } 336 else if (FieldNameStr == "REFERER:") { 337 RequestFields.RefererStr = FieldValStr; 338 } 339 else if (FieldNameStr == "IF-MODIFIED-SINCE:") { 340 RequestFields.IfModSinceStr = FieldValStr; 353 341 } 354 342 //--Uncommon-- 355 else if ( strcmp("FROM:", FieldNameStr) == 0) {356 strncpy(RequestFields.FromStr, FieldValStr, ReqFromStrLen - 1);357 } 358 else if ( strcmp("MIME-VERSION:", FieldNameStr) == 0) {359 strncpy(RequestFields.MIMEVerStr, FieldValStr, ReqMIMEVerStrLen - 1);360 } 361 else if ( strcmp("PRAGMA:", FieldNameStr) == 0) {362 strncpy(RequestFields.PragmaStr, FieldValStr, ReqPragmaStrLen - 1);343 else if (FieldNameStr == "FROM:") { 344 RequestFields.FromStr = FieldValStr; 345 } 346 else if (FieldNameStr == "MIME-VERSION:") { 347 RequestFields.MIMEVerStr = FieldValStr; 348 } 349 else if (FieldNameStr == "PRAGMA:") { 350 RequestFields.PragmaStr = FieldValStr; 363 351 } 364 352 //--Special case-- 365 else if ( strcmp("AUTHORIZATION:", FieldNameStr) == 0) {366 strncpy(RequestFields.AuthorizationStr, FieldValStr, ReqAuthorizationStrLen - 1);367 } 368 else if ( strcmp("CONTENT-LENGTH:", FieldNameStr) == 0) {369 strncpy(RequestFields.ContentLengthStr, FieldValStr, ReqContentLengthStrLen - 1);370 } 371 else if ( strcmp("CONTENT-TYPE:", FieldNameStr) == 0) {372 strncpy(RequestFields.ContentTypeStr, FieldValStr, ReqContentTypeStrLen - 1);373 } 374 else if ( strcmp("CONTENT-ENCODING:", FieldNameStr) == 0) {375 strncpy(RequestFields.ContentEncodingStr, FieldValStr, ReqContentEncodingStrLen - 1);376 } 377 else {353 else if (FieldNameStr == "AUTHORIZATION:") { 354 RequestFields.AuthorizationStr = FieldValStr; 355 } 356 else if (FieldNameStr == "CONTENT-LENGTH:") { 357 RequestFields.ContentLengthStr = FieldValStr; 358 } 359 else if (FieldNameStr == "CONTENT-TYPE:") { 360 RequestFields.ContentTypeStr = FieldValStr; 361 } 362 else if (FieldNameStr == "CONTENT-ENCODING:") { 363 RequestFields.ContentEncodingStr = FieldValStr; 364 } 365 else if (!FieldNameStr.empty()) { 378 366 //Add it to the other headers 379 int VarLen = strlen(FieldNameStr); 380 if (FieldNameStr[VarLen - 1] == ':') { 381 //Remove the colon 382 FieldNameStr[VarLen - 1] = '\0'; 383 VarLen--; 384 } 385 RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Var = new char[VarLen + 1]; 386 RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Val = new char[Len + 1]; 387 strcpy(RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Var, FieldNameStr); 388 strcpy(RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Val, FieldValStr); 367 368 //Remove the colon 369 if (*(FieldNameStr.end()-1) == ':') { 370 FieldNameStr.pop_back(); 371 } 372 RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Var = FieldNameStr; 373 RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Val = FieldValStr; 389 374 RequestFields.NumOtherHeaders++; 390 375 } 391 strcpy(CurLine, NextLine);392 } 393 394 if ( RequestFields.ContentLengthStr[0] != 0) { //Do we have attached data?376 CurLine = NextLine; 377 } 378 379 if (!RequestFields.ContentLengthStr.empty()) { //Do we have attached data? 395 380 unsigned int NumRecv; 396 381 397 RequestFields.ContentLength = atol(RequestFields.ContentLengthStr);382 RequestFields.ContentLength = RequestFields.ContentLengthStr.getint(); 398 383 if (RequestFields.ContentLength > 0) { 399 384 … … 447 432 void CleanUpHTTPHeaders(RequestInfoT &RequestInfo, RequestFieldsT &RequestFields) { 448 433 //Clean up memory allocated for the Content 449 if (RequestFields.Content != NULL) 434 if (RequestFields.Content != NULL) { 450 435 delete[] RequestFields.Content; 451 while (RequestFields.NumOtherHeaders > 0) {452 RequestFields.NumOtherHeaders--;453 delete[] RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Var;454 delete[] RequestFields.OtherHeaders[RequestFields.NumOtherHeaders].Val;455 436 } 456 437
Note:
See TracChangeset
for help on using the changeset viewer.