/* Clean up an error page if it needs it. */ void page_clean_up (PAGE *page) { BPRINTF_BUFFER *subber; subber = bprintf_create_buffer (); if (page_search (page, "<HEADER>", 0) != -1) { bprintf (subber, "<html><head><title>Error</title></head>\n"); bprintf (subber, "<body>\n"); page_subst_in_page (page, "<HEADER>", subber->buffer); subber->bindex = 0; } page_subst_in_page (page, "<ERROR-MESSAGE>", ""); page_subst_in_page (page, "<RETURN-TO-URL>", ""); if (debugger_output != (BPRINTF_BUFFER *)NULL) { bprintf (subber, "%s", debugger_output->buffer); page_subst_in_page (subber, "\n", "\n<br>"); bprintf_insert (subber, 0, "<h2>Output From Debugging Statements</h2><p>"); page_subst_in_page (page, "<DEBUGGING-OUTPUT-HTML>", subber->buffer); subber->bindex = 0; bprintf (subber, "%s", debugger_output->buffer); page_subst_in_page (subber, "<", "<"); page_subst_in_page (subber, ">", ">"); page_subst_in_page (subber, "\n", "\n<br>"); bprintf_insert (subber, 0, "<h2>Output From Debugging Statements</h2><p>"); page_subst_in_page (page, "<DEBUGGING-OUTPUT>", subber->buffer); subber->bindex = 0; } else page_subst_in_page (page, "<DEBUGGING-OUTPUT>", ""); if (syserr_output != (BPRINTF_BUFFER *)NULL) { bprintf (subber, "%s", syserr_output->buffer); page_subst_in_page (subber, "<", "<"); page_subst_in_page (subber, ">", ">"); page_subst_in_page (subber, "\n", "\n<br>"); bprintf_insert (subber, 0, "<h2>System Error Messages</h2><p>"); page_subst_in_page (page, "<SYSTEM-ERROR-OUTPUT>", subber->buffer); subber->bindex = 0; } else page_subst_in_page (page, "<SYSTEM-ERROR-OUTPUT>", ""); if (page_search (page, "<FOOTER>", 0) != -1) page_subst_in_page (page, "<FOOTER>", "</body></html>\n"); bprintf_free_buffer (subber); }
/* Set a mime header in PAGE giving it NAME and VALUE as a HTTP-COOKIE. PAGE should already have a full Mime header in it. */ void page_set_cookie (PAGE *page, char *name, char *value, char *path) { register int i; if (!page) return; i = page_search (page, "[Mm][Ii][Mm][Ee]-[Vv][Ee][Rr][Ss][Ii][Oo][Nn]: ", 0); if (i != -1) { BPRINTF_BUFFER *cookie_buffer = bprintf_create_buffer (); bprintf (cookie_buffer, "Set-Cookie: %s=%s; ", name, value ? value : ""); if (!value || !*value) { time_t ticks = time ((time_t *)0); ticks -= 1000; bprintf (cookie_buffer, " expires=%s; ", http_date_format (ticks)); } bprintf (cookie_buffer, "path=%s;", path && *path ? path : "/"); while (page->buffer[i++] != '\n'); bprintf_insert (page, i, "%s\n", cookie_buffer->buffer); } }
/* -- Also, when allocating a block - split it into two blocks when possible */ void* Mem_Alloc(int size) { // Just a quick check... if (spaceAvail < size || size == 0) { return NULL; } //fprintf(stdout, "spaceAvail: %d\n", spaceAvail); size = size + (8 - (size % 8))*((size % 8) != 0); /* Traverse free list until block of desired size is found or end of * list is reached*/ block_header* curr = page_search(size); // We got a useful result! if (curr != NULL) { int leftover = (curr->size_status) - (size + sizeof(block_header)); /* Put remaining mem in new block */ if (leftover > 0) { block_header* newblk = (block_header*)((char*)curr + sizeof(block_header) + size); newblk->size_status = leftover; if (newblk->size_status <= 0) fprintf(stdout, "Set to zero\n"); newblk->prev = curr; newblk->next = curr->next; curr->next = newblk; //fprintf(stdout, "In new block at Mem_Alloc\n"); } curr->size_status = size + 1; spaceAvail -= size + sizeof(block_header); /* fprintf(stdout, "Past new block at Mem_Alloc\n"); fprintf(stdout, "Value of curr at Mem_Alloc: %p\n", curr); fprintf(stdout, "Value of NULL at Mem_Alloc: %p\n", NULL); */ counter = (counter + 1) % N_LISTS; return (curr + 1); } /* Block of appropriate size not found */ //fprintf(stdout, "Returning null at Mem_Alloc\n"); return NULL; }
/* Insert a stnadard HTTP header at the start of page if it isn't already a redirection specification. EXPIRATION is one of: page_NOT_EXPIRED: To produce no special expiration date. page_IS_EXPIRED: To inhibit server/browser caching. page_EXPIRES_NOW: To give the page an expiration date of right now. integer > 0: To cause the page to expire that many minutes in the future. */ void page_insert_http_header (PAGE *page, int expiration) { if (page) { time_t ticks = time ((time_t *)0); BPRINTF_BUFFER *http_header = bprintf_create_buffer (); int redirection = 0; int end_of_headers = -1; int page_length = page->bindex; /* Find out if this is a location redirector. */ if (page->buffer && (strncasecmp (page->buffer, "Location:", 9) == 0)) end_of_headers = 0; else end_of_headers = page_search (page, "[^\n]\nLocation:", 0); redirection = (end_of_headers != -1); if (redirection) { while (end_of_headers < page_length - 1) { if ((page->buffer[end_of_headers] == '\n') && (page->buffer[end_of_headers + 1] == '\n')) { end_of_headers += 2; page_length = page->bindex - end_of_headers; break; } end_of_headers++; } } /* Say that this page is using a MIME version of 1.0. */ bprintf (http_header, "MIME-Version: 1.0\n"); /* Say that this page contains text/html. */ bprintf (http_header, "Content-Type: text/html\n"); /* Say how long the page is. */ bprintf (http_header, "Content-Length: %d\n", page_length); /* Say what time it is now. */ bprintf (http_header, "Date: %s\n", http_date_format (ticks)); /* Handle page expiration. */ switch (expiration) { case page_NOT_EXPIRED: break; case page_IS_EXPIRED: bprintf (http_header, "Status: 500\n"); break; default: { /* Adjust the contents of NOW to reflect the new date. */ if (expiration > 0) ticks += (60 * expiration); else ticks -= (60 * 60 * 24); /* Expired one day ago. */ bprintf (http_header, "Expires: %s\n", http_date_format (ticks)); } } /* Does this page already contain an "HTTP/1.0" line? If so, then insert our header after it. */ { register int i = 0; if (strncmp (page->buffer, "HTTP/", 5) == 0) { for (i = 5; page->buffer[i] != '\n'; i++); i++; } bprintf_insert (page, i, "%s%s", http_header->buffer, redirection ? "" : "\n"); } bprintf_free_buffer (http_header); } }