Ignore:
Timestamp:
1999-08-20T12:59:01+12:00 (25 years ago)
Author:
sjboddie
Message:

-fixed up location redirection
-added some usage logging, also now set a GSDL_UID cookie. Logging
does NOT presently lock the log file while it's in use. That has yet
to be done.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/recpt/receptionist.cpp

    r456 r463  
    1212/*
    1313   $Log$
     14   Revision 1.25  1999/08/20 00:59:01  sjboddie
     15   -fixed up location redirection
     16   -added some usage logging, also now set a GSDL_UID cookie. Logging
     17   does NOT presently lock the log file while it's in use. That has yet
     18   to be done.
     19
    1420   Revision 1.24  1999/08/13 04:16:42  sjboddie
    1521   added some collection-level metadata stuff
     
    114120#include <assert.h>
    115121#include <time.h>
    116 
     122#include <fstream.h>
    117123
    118124
     
    191197  ainfo.savedarginfo = cgiarginfo::must;
    192198  argsinfo.addarginfo (NULL, ainfo);
     199
     200  // the GSDL_UID (cookie)
     201  ainfo.shortname = "z";
     202  ainfo.longname = "gsdl uid";
     203  ainfo.multiplechar = true;
     204  ainfo.defaultstatus = cgiarginfo::none;
     205  ainfo.argdefault = "";
     206  ainfo.savedarginfo = cgiarginfo::mustnot;
     207  argsinfo.addarginfo (NULL, ainfo);
     208
    193209}
    194210
     
    382398  add_default_args (argsinfo, args, logout);
    383399
     400  // get the cookie
     401  get_cookie(args["z"]);
    384402 
    385403  // get the input encoding
     
    437455}
    438456
     457// returns true if cookie already existed, false
     458// if it was generated
     459bool receptionist::get_cookie (text_t &cookie) {
     460
     461  char *c = getenv("HTTP_COOKIE");
     462  if (c != NULL) {
     463    text_t cookiestring = c;
     464   
     465    text_t::const_iterator end = cookiestring.end(); 
     466    text_t::const_iterator here = findchar (cookiestring.begin(), end, 'G');
     467   
     468    if (substr(here, here+8) == "GSDL_UID") {
     469      cookie = substr (here+9, findchar (here+9, end, ';'));
     470      return true;
     471    }
     472  }
     473
     474  char *host = getenv("REMOTE_ADDR");
     475  time_t ttime = time(NULL);
     476  if (host != NULL) {
     477    cookie += host;
     478    cookie.push_back ('-');
     479  }
     480  cookie += text_t(ttime);
     481 
     482  return false;
     483}
     484
     485// as above but just tests if cookie exists
     486bool receptionist::get_cookie () {
     487
     488  char *c = getenv("HTTP_COOKIE");
     489  if (c != NULL) {
     490    text_t cookiestring = c;
     491   
     492    text_t::const_iterator end = cookiestring.end(); 
     493    text_t::const_iterator here = findchar (cookiestring.begin(), end, 'G');
     494   
     495    if (substr(here, here+8) == "GSDL_UID")
     496      return true;
     497  }
     498  return false;
     499}
     500
     501bool receptionist::log_cgi_args (cgiargsclass &args, ostream &logout) {
     502
     503  outconvertclass text_t2ascii;
     504
     505  char *host;
     506  host = getenv("REMOTE_HOST");
     507  if (host == NULL) host = getenv ("REMOTE_ADDR");
     508  char *browser = getenv("HTTP_USER_AGENT");
     509  time_t ttime = time(NULL);
     510
     511  cgiargsclass::const_iterator args_here = args.begin();
     512  cgiargsclass::const_iterator args_end = args.end();
     513
     514  text_t argstr;
     515  bool first = true;
     516  while (args_here != args_end) {
     517    if (!first) argstr += ", ";
     518    argstr += (*args_here).first + "=" + (*args_here).second.value;
     519    first = false;
     520    args_here ++;
     521  }
     522
     523  text_t logfile = filename_cat (configinfo.gsdlhome, "etc");
     524  logfile = filename_cat (logfile, "usage.txt");
     525  char *lfile = logfile.getcstr();
     526 
     527  ofstream log (lfile, ios::app);
     528 
     529  if (!log) {
     530    logout << "Error: Couldn't open file " << lfile << "\n";
     531    delete lfile;
     532    return false;
     533  }
     534
     535  log << text_t2ascii << host << " [" << ttime
     536      << "] (" << argstr << ") \"" << browser << "\"\n";
     537   
     538  log.close();
     539       
     540  delete lfile;
     541  return true;
     542}
    439543
    440544// produce_cgi_page will call get_cgihead_info and
     
    452556  get_cgihead_info (args, response, response_data, logout);
    453557  if (response == location) {
    454     // I've forgotten how to do this :-/
     558    // location response
     559    contentout << text_t2ascii << "Location: " << response_data << "\n\n";
     560    contentout << flush;
    455561    return true;
    456562  } else if (response == content) {
     
    501607      response_data += "; charset=ISO-8859-1";     
    502608    }
     609
     610    // add cookie if required
     611    if (!get_cookie())
     612      response_data += "\nSet-Cookie: GSDL_UID=" + args["z"]
     613    + "; expires=25-Dec-37 00:00:00 GMT";
     614   
    503615  }
    504616}
Note: See TracChangeset for help on using the changeset viewer.