Ignore:
Timestamp:
2003-03-09T09:08:58+13:00 (21 years ago)
Author:
sjboddie
Message:

Removed some hard string length limits in local library server code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/w32server/httpreq.cpp

    r3056 r3810  
    2626 *********************************************************************/
    2727
    28 #include "text_t.h"
    2928#include <windows.h>
    3029#include <stdlib.h>
     
    3231#include <string.h>
    3332#include <memory.h>
    34 #pragma hdrstop
     33#include "httpreq.h"
    3534#include "parse.h"
    3635#include "netio.h"
    3736#include "settings.h"
    38 #include "httpreq.h" //Had to put myself here because of the global types...
    3937#include "httpsrv.h"
    4038#include "httpsend.h"
     
    5452#define IO_BUFFER_SIZE 16384        //16K IO Buffer
    5553#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 line
    57 #define MAX_HTTP_FIELD_LEN 1024     //Max length of data in line
    5854
    5955//Private Function Declarations with Return Contstants
     
    219215int GetHTTPHeaders(RequestInfoT &RequestInfo, RequestFieldsT &RequestFields) {
    220216  //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;
    225221 
    226222  //Parsing and IO working vars
    227223  int ReadBufferIndex;
    228224  int DataInBuffer;
    229   int Start;
    230   int End;
    231   int Len;
     225  text_t::const_iterator next;
     226  text_t::const_iterator end;
    232227
    233228  //Clear all the fields
     
    245240           RequestInfo.IOBufferSize, ReadBufferIndex, DataInBuffer,
    246241           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'));
    250246  //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);
    254249
    255250  /* Added Feb 2002 - IE since about version 5 send stupid frontpage requests
    256251     for MS Document formats eg "GET /_vti_inf.html" */
    257   if (strcmp(RequestFields.MethodStr,"OPTIONS")==0) {
    258     return GH_BAD_METHOD;
     252  if (RequestFields.MethodStr == "OPTIONS") {
     253    return GH_BAD_METHOD;
    259254  }
    260255  //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/")) {
    265261    //No version, assume simple request
    266262    //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;
    270264    return GH_SIMPLE_REQUEST;
    271265  }
     
    273267  //URI String (in between End of first and Start of last)
    274268  //<Method> <WhiteSpace> <URI> <WhiteSpace> <Version> <CRLF>
    275   //                  End^             Start^
     269  //                  next^             end^
    276270  text_t spacebuffer;
    277   for (int i = End; i < Start; i++) {
     271  text_t::const_iterator here = next;
     272  while (here != end) {
    278273    // do this to remove trailing space
    279     if (CurLine[i] == ' ') {
    280       spacebuffer.push_back(' ');
     274    if (*here == ' ' || *here == '\t') {
     275      spacebuffer.push_back(*here);
    281276    } else {
    282277      if (!spacebuffer.empty()) {
     
    284279    spacebuffer.clear();
    285280      }
    286       RequestFields.URIStr.push_back(CurLine[i]);
    287     }
     281      RequestFields.URIStr.push_back(*here);
     282    }
     283    here++;
    288284  }
    289285
    290286  //Only accept requests from HTTP/0.9 or HTTP/1.X clients, we'll
    291287  //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.")) {
    293289    return GH_UNKNOWN_VERSION;
     290  }
    294291 
    295292  //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
    300296    do {//Get Next Line, append it if the first charactor is space
    301297      if (GetLine(NextLine, RequestInfo.ClientSocket, RequestInfo.IOBuffer,
     
    303299          RequestInfo.ThreadNum) != 0)
    304300    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'));
    308305   
    309     Start = 0;
    310     GetWord(FieldNameStr, CurLine, Start, End);
    311     CharUpper(FieldNameStr);
     306    GetWord(FieldNameStr, CurLine.begin(), CurLine.end(), next);
     307    uc(FieldNameStr);
    312308   
    313     Len = strlen(CurLine) - End;
    314     memcpy(FieldValStr, CurLine + End, Len);
    315     FieldValStr[Len] = 0;
     309    FieldValStr = substr(next, CurLine.end());
    316310   
    317311    //Process it
     
    319313    //All constants are in canonized, thus in upper case and case sensitive
    320314    //comparisons are used
     315
    321316    //--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;
    343331    }
    344332    //--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;
    353341    }
    354342    //--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;
    363351    }
    364352    //--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()) {
    378366      //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;
    389374      RequestFields.NumOtherHeaders++;
    390375    }
    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?
    395380    unsigned int NumRecv;
    396381   
    397     RequestFields.ContentLength = atol(RequestFields.ContentLengthStr);
     382    RequestFields.ContentLength = RequestFields.ContentLengthStr.getint();
    398383    if (RequestFields.ContentLength > 0) {
    399384
     
    447432void CleanUpHTTPHeaders(RequestInfoT &RequestInfo, RequestFieldsT &RequestFields) {
    448433  //Clean up memory allocated for the Content
    449   if (RequestFields.Content != NULL)
     434  if (RequestFields.Content != NULL) {
    450435    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;
    455436  }
    456437 
Note: See TracChangeset for help on using the changeset viewer.