Ignore:
Timestamp:
2001-04-05T17:08:52+12:00 (23 years ago)
Author:
sjboddie
Message:

Had a bit of a tidy up in the fnord webserver code. The main change of note
was the removal of our reliance on the MAX_URL_SIZE constant. URLs and post
data of any length should now work.

File:
1 edited

Legend:

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

    r2280 r2286  
    172172  return 0;
    173173}
    174 
    175 
    176 
    177 #ifndef MAX_FILENAME_SIZE
    178 #define MAX_FILENAME_SIZE 2048
    179 #endif
    180 
    181174
    182175receptionist recpt;
     
    580573
    581574
    582 static void rememberpref (char *tailstr) {
    583 
    584   //  gsdl_enterlib = tailstr;
    585 }
    586 
    587 
    588 static void send_file_from_disk(char *filename,
     575static void rememberpref (const text_t &tailstr) {
     576  gsdl_enterlib = tailstr;
     577}
     578
     579
     580static void send_file_from_disk(text_t filename,
    589581                                RequestInfoT *RInfo,
    590                                 RequestFieldsT *RFields)
    591 {
    592   int len, nr;  char *tail, *kind;
    593   FILE *thefile; 
    594  
    595   // select appropriate mime type from file name
    596   len = strlen(filename);
    597   while (len > 0 && filename[len-1] != '.') len--;
    598   kind = "unknown";
    599   if (len > 0) {
    600     tail = &filename[len];
    601     if (stricmp("gif",tail) == 0) kind = "image/gif";
    602     else if (stricmp("jpg",tail) == 0 ||
    603              stricmp("jpeg",tail) == 0) kind = "image/jpeg";
    604     else if (stricmp("htm",tail) == 0 ||
    605              stricmp("html",tail) == 0) kind = "text/html";
    606   }
    607 
    608   // set up file name
    609   text_t filenamet = filename_cat (current_gsdlhome, filename);
    610   filename = filenamet.getcstr();
     582                                RequestFieldsT *RFields) {
     583 
     584  // select appropriate mime type from file extension
     585  text_t ext;
     586  text_t::const_iterator end = filename.end();
     587  text_t::const_iterator it = filename.begin();
     588  text_t::const_iterator lastdot = end;
     589  while ((it = findchar(it, end, '.')) != end) {
     590    lastdot = it;
     591    it++;
     592  }
     593  if (lastdot < end) ext = substr(lastdot+1, end);
     594
     595  text_t mime = "unknown";
     596  int len = ext.size();
     597  if (len == 3) {
     598    if ((ext[0] == 'g' || ext[0] == 'G') &&
     599    (ext[1] == 'i' || ext[1] == 'I') &&
     600    (ext[2] == 'f' || ext[2] == 'F')) {
     601      mime = "image/gif";
     602    } else if ((ext[0] == 'j' || ext[0] == 'J') &&
     603           (ext[1] == 'p' || ext[1] == 'P') &&
     604           (ext[2] == 'g' || ext[2] == 'G')) {
     605      mime = "image/jpeg";
     606    } else if ((ext[0] == 'h' || ext[0] == 'H') &&
     607           (ext[1] == 't' || ext[1] == 'T') &&
     608           (ext[2] == 'm' || ext[2] == 'M')) {
     609      mime = "text/html";
     610    }
     611  } else if (len == 4) {
     612    if ((ext[0] == 'j' || ext[0] == 'J') &&
     613    (ext[1] == 'p' || ext[1] == 'P') &&
     614    (ext[2] == 'e' || ext[2] == 'E') &&
     615    (ext[3] == 'g' || ext[3] == 'G')) {
     616      mime = "image/jpeg";
     617    } else if ((ext[0] == 'h' || ext[0] == 'H') &&
     618           (ext[1] == 't' || ext[1] == 'T') &&
     619           (ext[2] == 'm' || ext[2] == 'M') &&
     620           (ext[3] == 'l' || ext[3] == 'L')) {
     621      mime = "text/html";
     622    }
     623  }
    611624   
    612   // try to open it
    613   thefile = fopen(filename, "rb");
     625  // try to open the file
     626  filename = filename_cat (current_gsdlhome, filename);
     627  char *filenamec = filename.getcstr();
     628  FILE *thefile = fopen(filenamec, "rb");
     629  delete filenamec;
    614630  if (thefile == NULL) {
    615631    log_message("file not found\n");
     
    618634    return;
    619635  }
    620  
     636
     637  int nr;
    621638  char buffer[2048];
    622639  // send back required information
    623   if (send_header(kind, RInfo) >= 0) {
     640  char *mimec = mime.getcstr();
     641  if (send_header(mimec, RInfo) >= 0) {
    624642    if (strcmpi(RFields->MethodStr, "HEAD") != 0) {
    625643      for (;;) {
     
    632650    }
    633651  }
     652  delete mimec;
    634653  fclose(thefile);
    635654}
    636655
    637 static void handle_library_request(char *TailStr, RequestInfoT *RInfo,
     656static void handle_library_request(const text_t &argstr, RequestInfoT *RInfo,
    638657                   RequestFieldsT *RequestFields) {
    639   // check for a "?" at the start of the tail string
    640   if (*TailStr == '?') TailStr++;
    641 
    642   outconvertclass text_t2ascii;
    643  
     658
    644659  // parse the cgi arguments and produce the resulting page if there
    645   // has been no errors so far
     660  // have been no errors so far
    646661  cgiargsclass args;
    647662  text_tmap empty; // don't use this (it's for fastcgi on unix)
    648   if (!recpt.parse_cgi_args (TailStr, args, cerr, empty)) {
     663  if (!recpt.parse_cgi_args (argstr, args, cerr, empty)) {
    649664    page_errorparseargs(gsdl_gsdlhome);
    650665    return;
     
    700715}
    701716
    702 static void handle_server_request(char *initialTailStr,
     717static void handle_server_request(text_t &tailstr,
    703718                  RequestInfoT *RequestInfo,
    704719                  RequestFieldsT *RequestFields) {
    705   char tmpstr[MAX_URL_SIZE+1024];
    706   char tailstr[MAX_URL_SIZE+16];
    707  
     720 
     721  text_t argstr;
     722
    708723  // do any url adjustments necessary
    709   if (strcmp(initialTailStr, "/") == 0) {
    710     strcpy (tailstr, "/cgi-bin/gw");
    711   } else strcpy (tailstr, initialTailStr);
     724  if (tailstr.empty() || tailstr == "/") {
     725    tailstr = "/cgi-bin/gw";
     726  }
     727
     728  text_t::const_iterator begin = tailstr.begin();
     729  text_t::const_iterator end = tailstr.end();
    712730 
    713731  // test to see if this is a library request or a local
    714732  // file request
    715   if ((strncmp(tailstr, "/cgi-bin/gw", 11) == 0) &&
    716       ((tailstr[11] == '\0') || (tailstr[11] == '?'))) {
     733  if ((tailstr == "/cgi-bin/gw") ||
     734      ((tailstr.size() > 11) &&
     735       (substr(begin, begin+11) == "/cgi-bin/gw"))) {
     736
    717737    // library request
    718738   
     739    // argstr is the bit after the '?'
     740    if (tailstr != "/cgi-bin/gw") {
     741      argstr = substr(begin+12, end);
     742    }
     743
    719744    // log the difference in access times
    720745    DWORD thislibaccesstime = GetTickCount();
    721     if (gsdl_keep_log||gsdl_show_console) {
    722       sprintf(tmpstr, "DELTA LIB ACCESS TIME: %i\n", (int)(thislibaccesstime - lastlibaccesstime));
    723       log_message (tmpstr);
     746    if (gsdl_keep_log || gsdl_show_console) {
     747      char logstr[256];
     748      sprintf(logstr, "DELTA LIB ACCESS TIME: %i\n", (int)(thislibaccesstime - lastlibaccesstime));
     749      log_message (logstr);
    724750    }
    725751    lastlibaccesstime = thislibaccesstime;
    726752   
    727753    // log this request
    728     if (gsdl_keep_log||gsdl_show_console) {
    729       sprintf (tmpstr, "LOCAL LIB: %s\n", tailstr);
    730       log_message (tmpstr);
    731     }
    732    
    733     handle_library_request (&tailstr[11], RequestInfo, RequestFields);
     754    if (gsdl_keep_log || gsdl_show_console) {
     755      text_t logstr = "LOCAL LIB: " + tailstr + "\n";
     756      char *logstrc = logstr.getcstr();
     757      log_message (logstrc);
     758      delete logstrc;
     759    }
     760   
     761    handle_library_request (argstr, RequestInfo, RequestFields);
    734762   
    735763    // remember the preferences
    736     rememberpref (tailstr);
     764    // rememberpref (tailstr);
    737765   
    738766    // log memory information
    739     if (gsdl_keep_log||gsdl_show_console) {
     767    if (gsdl_keep_log || gsdl_show_console) {
    740768      MEMORYSTATUS memstatus;
    741769      memstatus.dwLength = sizeof(MEMORYSTATUS);
    742770      GlobalMemoryStatus(&memstatus);
    743       sprintf (tmpstr, "BDELTA AVAIL VIRTUAL: %i K\n",
     771      char logstr[256];
     772      sprintf (logstr, "BDELTA AVAIL VIRTUAL: %i K\n",
    744773           (int)((baseavailvirtual - memstatus.dwAvailVirtual)/1024));
    745       log_message (tmpstr);
     774      log_message (logstr);
    746775    }
    747776   
    748777  } else {
    749778    // local file
    750     if (gsdl_keep_log||gsdl_show_console) {
    751       sprintf (tmpstr, "LOCAL FILE: %s\n", tailstr);
    752       log_message (tmpstr);
     779    if (gsdl_keep_log || gsdl_show_console) {
     780      text_t logstr = "LOCAL FILE: " + tailstr + "\n";
     781      char *logstrc = logstr.getcstr();
     782      log_message (logstrc);
     783      delete logstrc;
    753784    }
    754785    send_file_from_disk (tailstr, RequestInfo, RequestFields);
    755786  }
    756 }
    757 
    758 static void fix_prefix(char *dest, char *pref, char *suff)
    759 {
    760   strcpy(dest,pref);
    761   if (*suff != '/') strcat(dest,"/");
    762   if (strlen(dest) + strlen(suff) + 1 > MAX_URL_SIZE)
    763     strcpy(dest,"http://gsdl/name-too-long");
    764   else
    765     strcat(dest,suff);
    766787}
    767788
     
    769790          RequestFieldsT *RequestFields)
    770791{
    771   char *protocol, *machine, *rest;  int port;
    772   //  char URICopyA[MAX_URL_SIZE], URICopyB[MAX_URL_SIZE];
     792  text_t protocol, machine, rest;
     793  int port;
    773794
    774795  if (RequestFields->ContentLength > 0) {
    775796    // POST data
    776797    URIStr.push_back('?');
    777     URIStr += (char*)RequestFields->Content;
    778 
    779     //    int len = strlen (URIStr);
    780     // fail relatively gracefully (and mysteriously) if POST
    781     // arguments are too long
    782     //    if (len + RequestFields->ContentLength + 1 <= MAX_URL_SIZE) {
    783     //      URIStr[len] = '?'; len ++;
    784     //      for (int i = 0; i < RequestFields->ContentLength; i++) {
    785     //  URIStr[len] = RequestFields->Content[i];
    786     //  len ++;
    787     //      }
    788     //      URIStr[len] = '\0';
    789     //    } else {
    790     //      MessageBox (NULL, "POST data too long", "Greenstone Digital Library Software", MB_OK);
    791     //    }
    792   }
    793  
    794   char *URICopyA = URIStr.getcstr();
    795   char URICopyB[MAX_URL_SIZE];
    796   char *URICopytmp = URIStr.getcstr();
    797 
    798     //  strcpy(URICopyA,URIStr);
    799  
    800   if (parse_url(URICopyA,&protocol,&machine,&port,&rest)!=http_ok) {
    801     /* Alter local file request to address 'gsdl' */
    802     //    fix_prefix(URICopyB, "http://gsdl", URIStr);
    803     fix_prefix(URICopyB, "http://gsdl", URICopytmp);
    804     URIStr = URICopyB;
    805     strcpy(URICopyA, URICopyB);
    806     parse_url(URICopyA,&protocol,&machine,&port,&rest);
    807   }
    808 
    809   if (strncmp(machine, "gsdl", 5) == 0) {
     798    for (int i = 0; i < RequestFields->ContentLength; i++) {
     799      URIStr.push_back(RequestFields->Content[i]);
     800    }
     801  }
     802 
     803  if (parse_url(URIStr, protocol, machine, &port, rest)!=http_ok) {
     804    // Alter local file request to address 'gsdl'
     805    if (*(URIStr.begin()) != '/') URIStr = "http://gsdl/" + URIStr;
     806    else URIStr = "http://gsdl" + URIStr;
     807    parse_url(URIStr, protocol, machine, &port, rest);
     808  }
     809
     810  if (machine == "gsdl") {
    810811    // a local file request
    811812    handle_server_request(rest, RequestInfo, RequestFields);
Note: See TracChangeset for help on using the changeset viewer.