home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / HTTPD / HTTPD_SO.TAR / httpd_1.3 / src / http_log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  6.6 KB  |  236 lines

  1. /*
  2.  * http_log.c: Dealing with the logs and errors
  3.  * 
  4.  * Rob McCool
  5.  * 
  6.  */
  7.  
  8.  
  9. #include "httpd.h"
  10.  
  11. FILE *error_log;
  12. static FILE *xfer_log;
  13.  
  14. void open_logs() {
  15.     if(!(error_log = fopen(error_fname,"a"))) {
  16.         fprintf(stderr,"httpd: could not open error log file %s.\n",
  17.                 error_fname);
  18.         perror("fopen");
  19.         exit(1);
  20.     }
  21.     if(!(xfer_log = fopen(xfer_fname,"a"))) {
  22.         fprintf(stderr,"httpd: could not open transfer log file %s.\n",
  23.                 xfer_fname);
  24.         perror("fopen");
  25.         exit(1);
  26.     }
  27. }
  28.  
  29. void close_logs() {
  30.     fclose(xfer_log);
  31.     fclose(error_log);
  32. }
  33.  
  34. void error_log2stderr() {
  35.     if(fileno(error_log) != STDERR_FILENO)
  36.         dup2(fileno(error_log),STDERR_FILENO);
  37. }
  38.  
  39. void log_pid() {
  40.     FILE *pid_file;
  41.  
  42.     if(!(pid_file = fopen(pid_fname,"w"))) {
  43.         fprintf(stderr,"httpd: could not log pid to file %s\n",pid_fname);
  44.         exit(1);
  45.     }
  46.     fprintf(pid_file,"%d\n",getpid());
  47.     fclose(pid_file);
  48. }
  49.  
  50. static char the_request[HUGE_STRING_LEN];
  51. int status;
  52. int bytes_sent;
  53.  
  54. void record_request(char *cmd_line) {
  55.     status = -1;
  56.     bytes_sent = -1;
  57.  
  58.     strcpy(the_request,cmd_line);
  59. }
  60.  
  61. void log_transaction() {
  62.     char str[HUGE_STRING_LEN];
  63.     long timz;
  64.     struct tm *t;
  65.     char tstr[MAX_STRING_LEN],sign;
  66.  
  67.     t = get_gmtoff(&timz);
  68.     sign = (timz < 0 ? '-' : '+');
  69.     if(timz < 0) 
  70.         timz = -timz;
  71.  
  72.     strftime(tstr,MAX_STRING_LEN,"%d/%b/%Y:%H:%M:%S",t);
  73.  
  74.     sprintf(str,"%s %s %s [%s %c%02d%02d] \"%s\" ",
  75.             remote_name,
  76.             (do_rfc931 ? remote_logname : "-"),
  77.             (user[0] ? user : "-"),
  78.             tstr,
  79.             sign,
  80.             timz/3600,
  81.             timz%3600,
  82.             the_request);
  83.     if(status != -1)
  84.         sprintf(str,"%s%d ",str,status);
  85.     else
  86.         strcat(str,"- ");
  87.  
  88.     if(bytes_sent != -1)
  89.         sprintf(str,"%s%d",str,bytes_sent);
  90.     else
  91.         strcat(str,"- ");
  92.     fprintf(xfer_log,"%s\n",str);
  93.     fclose(xfer_log);
  94. }
  95.  
  96. void log_error(char *err) {
  97.     fprintf(error_log, "[%s] %s\n",get_time(),err);
  98.     fclose(error_log);
  99. }
  100.  
  101. void log_error_noclose(char *err) {
  102.     fprintf(error_log, "[%s] %s\n",get_time(),err);
  103.     fflush(error_log);
  104. }
  105.  
  106. void log_reason(char *reason, char *file) {
  107.     char t[MAX_STRING_LEN];
  108.  
  109.     sprintf(t,"httpd: access to %s failed for %s, reason: %s",
  110.             file,remote_name,reason);
  111.     log_error(t);
  112. }
  113.  
  114. void begin_http_header(FILE *fd, char *msg) {
  115.     fprintf(fd,"%s %s%c",SERVER_PROTOCOL,msg,LF);
  116.     dump_default_header(fd);
  117. }
  118.  
  119. void error_head(FILE *fd, char *err) {
  120.     if(!assbackwards) {
  121.         begin_http_header(fd,err);
  122.         fprintf(fd,"Content-type: text/html%c%c",LF,LF);
  123.     }
  124.     if(!header_only) {
  125.         fprintf(fd,"<HEAD><TITLE>%s</TITLE></HEAD>%c",err,LF);
  126.         fprintf(fd,"<BODY><H1>%s</H1>%c",err,LF);
  127.     }
  128. }
  129.  
  130. void title_html(FILE *fd, char *msg) {
  131.     fprintf(fd,"<HEAD><TITLE>%s</TITLE></HEAD>%c",msg,LF);
  132.     fprintf(fd,"<BODY><H1>%s</H1>%c",msg,LF);
  133. }
  134.  
  135. void die(int type, char *err_string, FILE *fd) {
  136.     char t[MAX_STRING_LEN];
  137.  
  138.     switch(type) {
  139.       case REDIRECT:
  140.         status = 302;
  141.         if(!assbackwards) {
  142.             begin_http_header(fd,"302 Found");
  143.             fprintf(fd,"Location: %s%c",err_string,LF);
  144.             fprintf(fd,"Content-type: text/html%c",LF);
  145.             fputc(LF,fd);
  146.         }
  147.         if(header_only) break;
  148.         title_html(fd,"Document moved");
  149.         fprintf(fd,"This document has moved <A HREF=\"%s\">here</A>.<P>%c",
  150.                 err_string,LF);
  151.         break;
  152.       case USE_LOCAL_COPY:
  153.         status = USE_LOCAL_COPY;
  154.         begin_http_header(fd,"304 Not modified");
  155.         fputc(LF,fd);
  156.         header_only = 1;
  157.         break;
  158.       case AUTH_REQUIRED:
  159.         status = 401;
  160.         if(!assbackwards) {
  161.             begin_http_header(fd,"401 Unauthorized");
  162.             fprintf(fd,"Content-type: text/html%c",LF);
  163.             fprintf(fd,"WWW-Authenticate: %s%c%c",err_string,LF,LF);
  164.         }
  165.         if(header_only) break;
  166.         title_html(fd,"Authorization Required");
  167.         fprintf(fd,"Browser not authentication-capable or %c",LF);
  168.         fprintf(fd,"authentication failed.%c",LF);
  169.         break;
  170.       case BAD_REQUEST:
  171.         status = 400;
  172.         error_head(fd,"400 Bad Request");
  173.         if(header_only) break;
  174.         fprintf(fd,"Your client sent a query that this server could not%c",LF);
  175.         fprintf(fd,"understand.<P>%c",LF);
  176.         fprintf(fd,"Reason: %s<P>%c",err_string,LF);
  177.         break;
  178.       case FORBIDDEN:
  179.         status = 403;
  180.         error_head(fd,"403 Forbidden");
  181.         if(header_only) break;
  182.         fprintf(fd,"Your client does not have permission to get URL %s ",
  183.                 err_string);
  184.         fprintf(fd,"from this server.<P>%c",LF);
  185.         break;
  186.       case NOT_FOUND:
  187.         status = 404;
  188.         error_head(fd,"404 Not Found");
  189.         if(header_only) break;
  190.         fprintf(fd,"The requested URL %s was not found on this server.<P>%c",
  191.                 err_string,LF);
  192.         break;
  193.       case SERVER_ERROR:
  194.         status = 500;
  195.         error_head(fd,"500 Server Error");
  196.         log_error(err_string);
  197.         if(header_only) 
  198.             break;
  199.         fprintf(fd,"The server encountered an internal error or%c",LF);
  200.         fprintf(fd,"misconfiguration and was unable to complete your%c",LF);
  201.         fprintf(fd,"request.<P>%c",LF);
  202.         fprintf(fd,"Please contact the server administrator,%c",LF);
  203.         fprintf(fd," %s ",server_admin);
  204.         fprintf(fd,"and inform them of the time the error occurred, and%c",LF);
  205.         fprintf(fd,"anything you might have done that may have caused%c",LF);
  206.         fprintf(fd,"the error.<P>%c",LF);
  207.         break;
  208.       case NOT_IMPLEMENTED:
  209.         status = 501;
  210.         error_head(fd,"501 Not Implemented");
  211.         if(header_only) break;
  212.         fprintf(fd,"We are sorry to be unable to perform the method %s",
  213.                 err_string);
  214.         fprintf(fd," at this time.<P>%c",LF);
  215.         fprintf(fd,"If you would like to see this capability in future%c",LF);
  216.         fprintf(fd,"releases, send the method which failed, why you%c",LF);
  217.         fprintf(fd,"would like to have it, and the server version %s%c",
  218.                 SERVER_VERSION,LF);
  219.         fprintf(fd,"to <ADDRESS>%s</ADDRESS><P>%c",SERVER_SUPPORT,LF);
  220.         break;
  221.       case NO_MEMORY:
  222.         log_error("httpd: memory exhausted");
  223.         status = 500;
  224.         error_head(fd,"500 Server Error");
  225.         if(header_only) break;
  226.         fprintf(fd,"The server has temporarily run out of resources for%c",LF);
  227.         fprintf(fd,"your request. Please try again at a later time.<P>%c",LF);
  228.         break;
  229.     }
  230.     if(!header_only)
  231.         fprintf(fd,"</BODY>%c",LF);
  232.     fflush(fd);
  233.     log_transaction();
  234.     htexit(1,fd);
  235. }
  236.