+/*********************************************************************
+ *
+ * Function : cgi_send_user_manual
+ *
+ * Description : CGI function that sends a file in the user
+ * manual directory.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : rsp = http_response data structure for output
+ * 3 : parameters = map of cgi parameters
+ *
+ * CGI Parameters : file=name.html, the name of the HTML file
+ * (relative to user-manual from config)
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+jb_err cgi_send_user_manual(struct client_state *csp,
+ struct http_response *rsp,
+ const struct map *parameters)
+{
+ const char * filename;
+ char *full_path;
+ FILE *fp;
+ 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, '/'))
+ {
+ return JB_ERR_CGI_PARAMS;
+ }
+ if (filename && strstr(filename, ".."))
+ {
+ return JB_ERR_CGI_PARAMS;
+ }
+
+ full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
+ if (full_path == NULL)
+ {
+ 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;
+ }
+
+ /* Get file length */
+ fseek(fp, 0, SEEK_END);
+ length = (size_t)ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ /* Allocate memory and load the file directly into the body */
+ rsp->body = (char *)zalloc(length+1);
+ if (!rsp->body)
+ {
+ 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.
+ */
+ log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
+ }
+ fclose(fp);
+ free(full_path);
+
+ rsp->content_length = length;
+
+ /* Guess correct Content-Type based on the filename's ending */
+ if (filename)
+ {
+ 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 err;
+}