-/*********************************************************************
- *
- * Function : make_http_response
- *
- * Description : Fill in the missing headers in an http response,
- * and flatten the headers to an http head.
- *
- * Parameters :
- * 1 : rsp = pointer to http_response to be processed
- *
- * Returns : length of http head, or 0 on failiure
- *
- *********************************************************************/
-int make_http_response(struct http_response *rsp)
-{
- char buf[BUFFER_SIZE];
-
- /* Fill in the HTTP Status */
- sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
- enlist_first(rsp->headers, buf);
-
- /* Set the Content-Length */
- if (rsp->content_length == 0)
- {
- rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
- }
-
-
- sprintf(buf, "Content-Length: %d", rsp->content_length);
- enlist(rsp->headers, buf);
-
- /* Fill in the default headers FIXME: Are these correct? sequence OK? check rfc! */
- enlist_unique(rsp->headers, "Pragma: no-cache", 7);
- enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
- enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
- enlist_unique(rsp->headers, "Content-Type: text/html", 13);
- enlist(rsp->headers, "");
-
-
- /* Write the head */
- if (NULL == (rsp->head = list_to_text(rsp->headers)))
- {
- free_http_response(rsp);
- return(0);
- }
-
- return(strlen(rsp->head));
-}
-
-
-/*********************************************************************
- *
- * Function : free_http_response
- *
- * Description : Free the memory occupied by an http_response
- * and its depandant structures.
- *
- * Parameters :
- * 1 : rsp = pointer to http_response to be freed
- *
- * Returns : N/A
- *
- *********************************************************************/
-void free_http_response(struct http_response *rsp)
-{
- if(rsp)
- {
- freez(rsp->status);
- freez(rsp->head);
- freez(rsp->body);
- destroy_list(rsp->headers);
- }
-}
-
-
-/*********************************************************************
- *
- * Function : fill_template
- *
- * Description : CGI support function that loads a given HTML
- * template from the confdir, and fills it in
- * by replacing @name@ with value using pcrs,
- * for each item in the output map.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- * 3 : template = name of the HTML template to be used
- * 2 : answers = map with fill in symbol -> name pairs
- * FIXME: needs better name!
- *
- * Returns : char * with filled out form, or NULL if failiure
- *
- *********************************************************************/
-char *fill_template(struct client_state *csp, char *template, struct map *answers)
-{
- struct map *m;
- pcrs_job *job, *joblist = NULL;
- char buf[BUFFER_SIZE];
- char *new, *old = NULL;
- int size;
- FILE *fp;
-
- /*
- * Open template file or fail
- */
- snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, template);
-
- if(NULL == (fp = fopen(buf, "r")))
- {
- log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
- return NULL;
- }
-
- /*
- * Assemble pcrs joblist from answers map
- */
- for (m = answers; m; m = m->next)
- {
- int error;
-
- snprintf(buf, BUFFER_SIZE, "s°@%s@°%s°ig", m->name, m->value);
-
- if(NULL == (job = pcrs_make_job(buf, &error)))
- {
- log_error(LOG_LEVEL_ERROR, "Adding template fill job %s failed with error %d",
- buf, error);
- while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
- return NULL;
- }
- else
- {
- job->next = joblist;
- joblist = job;
- }
- }
-
- /*
- * Read the file, ignoring comments
- */
- while (fgets(buf, BUFFER_SIZE, fp))
- {
- /* skip lines starting with '#' */
- if(*buf == '#') continue;
-
- old = strsav(old, buf);
- }
- fclose(fp);
-
- /*
- * Execute the jobs
- */
- size = strlen(old) + 1;
- new = old;
-
- for (job = joblist; NULL != job; job = job->next)
- {
- pcrs_exec_substitution(job, old, size, &new, &size);
- if (old != buf) free(old);
- old=new;
- }
-
- /*
- * Free the jobs & return
- */
- while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
- return(new);
-
-}
-
-
-/*********************************************************************
- *
- * Function : dump_map
- *
- * Description : HTML-dump a map for debugging
- *
- * Parameters :
- * 1 : map = map to dump
- *
- * Returns : string with HTML
- *
- *********************************************************************/
-char *dump_map(struct map *map)
-{
- struct map *p = map;
- char *ret = NULL;
-
-
- ret = strsav(ret, "<table>\n");
-
- while (p)
- {
- ret = strsav(ret, "<tr><td><b>");
- ret = strsav(ret, p->name);
- ret = strsav(ret, "</b></td><td>");
- ret = strsav(ret, p->value);
- ret = strsav(ret, "</td></tr>\n");
- p = p->next;
- }
-
- ret = strsav(ret, "</table>\n");
- return(ret);
-}
-
-