Merge in Debian changes from Debian packages 3.0.5-beta-3 and 3.0.6-1.
[privoxy.git] / cgisimple.c
index ffa734e..df5645d 100644 (file)
@@ -1,4 +1,4 @@
-const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.37 2006/07/18 14:48:45 david__schmidt Exp $";
+const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.41 2006/10/09 19:18:28 roro Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
@@ -36,6 +36,29 @@ const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.37 2006/07/18 14:48:45 david_
  *
  * Revisions   :
  *    $Log: cgisimple.c,v $
+ *    Revision 1.41  2006/10/09 19:18:28  roro
+ *    Redirect http://p.p/user-manual (without trailing slash) to
+ *    http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
+ *
+ *    Revision 1.40  2006/09/09 13:05:33  fabiankeil
+ *    Modified cgi_send_user_manual to serve binary
+ *    content without destroying it first. Should also be
+ *    faster now. Added ".jpg" check for Content-Type guessing.
+ *
+ *    Revision 1.39  2006/09/08 09:49:23  fabiankeil
+ *    Deliver documents in the user-manual directory
+ *    with "Content-Type text/css" if their filename
+ *    ends with ".css".
+ *
+ *    Revision 1.38  2006/09/06 18:45:03  fabiankeil
+ *    Incorporate modified version of Roland Rosenfeld's patch to
+ *    optionally access the user-manual via Privoxy. Closes patch 679075.
+ *
+ *    Formatting changed to Privoxy style, added call to
+ *    cgi_error_no_template if the requested file doesn't
+ *    exist and modified check whether or not Privoxy itself
+ *    should serve the manual. Should work cross-platform now.
+ *
  *    Revision 1.37  2006/07/18 14:48:45  david__schmidt
  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
  *    with what was really the latest development (the v_3_0_branch branch)
@@ -664,7 +687,8 @@ jb_err cgi_send_stylesheet(struct client_state *csp,
  *
  * Function    :  cgi_send_user_manual
  *
- * Description :  CGI function that sends a user manual HTML file
+ * Description :  CGI function that sends a file in the user
+ *                manual directory.
  *
  * Parameters  :
  *          1  :  csp = Current client state (buffers, headers, etc...)
@@ -685,13 +709,19 @@ jb_err cgi_send_user_manual(struct client_state *csp,
    const char * filename;
    char *full_path;
    FILE *fp;
-   char buf[BUFFER_SIZE];
    jb_err err = JB_ERR_OK;
+   size_t length;
 
    assert(csp);
    assert(rsp);
    assert(parameters);
 
+   if (!parameters->first)
+   {
+      /* requested http://p.p/user-manual (without trailing slash) */
+      return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
+   }
+
    get_string_param(parameters, "file", &filename);
    /* Check paramter for hack attempts */
    if (filename && strchr(filename, '/'))
@@ -709,41 +739,73 @@ jb_err cgi_send_user_manual(struct client_state *csp,
       return JB_ERR_MEMORY;
    }
 
-   /* Allocate buffer */
-   rsp->body = strdup("");
-   if (rsp->body == NULL)
-   {
-      free(full_path);
-      return JB_ERR_MEMORY;
-   }
-
    /* Open user-manual file */
+#ifdef WIN32
+   /*
+    * XXX: Do we support other operating systems that
+    * require special treatment to fopen in binary mode?
+    */
+   if (NULL == (fp = fopen(full_path, "rb")))
+#else
    if (NULL == (fp = fopen(full_path, "r")))
+#endif /* def WIN32 */
    {
       log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
       err = cgi_error_no_template(csp, rsp, full_path);
       free(full_path);
       return err;
    }
-   free(full_path);
 
-   /* Read file and write it out */
-   while (fgets(buf, BUFFER_SIZE, fp))
+   /* Get file length */
+   fseek(fp, 0, SEEK_END);
+   length = ftell(fp);
+   fseek(fp, 0, SEEK_SET);
+
+   /* Allocate memory and load the file directly into the body */
+   rsp->body = (char *)malloc(length+1);
+   if (!rsp->body)
    {
-      if (string_append(&rsp->body, buf))
-      {
-         fclose(fp);
-         return JB_ERR_MEMORY;
-      }
+      fclose(fp);
+      free(full_path);
+      return JB_ERR_MEMORY;
+   }
+   if (!fread(rsp->body, length, 1, fp))
+   {
+      /*
+       * This happens if we didn't fopen in binary mode.
+       * If it does, we just log it and serve what we got,
+       * most likely padded with garbage.
+       */
+      log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
    }
    fclose(fp);
+   free(full_path);
 
-   if (enlist(rsp->headers, "Content-Type: text/html"))
+   rsp->content_length = (int)length;
+
+   /* Guess correct Content-Type based on the filename's ending */
+   if (filename)
    {
-      return JB_ERR_MEMORY;
+      length = strlen(filename);
+   }
+   else
+   {
+      length = 0;
+   } 
+   if((length>=4) && !strcmp(&filename[length-4], ".css"))
+   {
+      err = enlist(rsp->headers, "Content-Type: text/css");
+   }
+   else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
+   {
+      err = enlist(rsp->headers, "Content-Type: image/jpeg");
+   }
+   else
+   {
+      err = enlist(rsp->headers, "Content-Type: text/html");
    }
 
-   return JB_ERR_OK;
+   return err;
 }