/* Dump the file upload details. Don't actually do anything with the uploaded file. */ static void uploadTest(Webs *wp, char *path, char *query) { WebsKey *s; WebsUpload *up; char *upfile; websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteHeader(wp, "Content-Type", "text/plain"); websWriteEndHeaders(wp); if (scaselessmatch(wp->method, "POST")) { for (s = hashFirst(wp->files); s; s = hashNext(wp->files, s)) { up = s->content.value.symbol; websWrite(wp, "FILE: %s\r\n", s->name.value.string); websWrite(wp, "FILENAME=%s\r\n", up->filename); websWrite(wp, "CLIENT=%s\r\n", up->clientFilename); websWrite(wp, "TYPE=%s\r\n", up->contentType); websWrite(wp, "SIZE=%d\r\n", up->size); upfile = sfmt("%s/tmp/%s", websGetDocuments(), up->clientFilename); rename(up->filename, upfile); wfree(upfile); } websWrite(wp, "\r\nVARS:\r\n"); for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) { websWrite(wp, "%s=%s\r\n", s->name.value.string, s->content.value.string); } } websDone(wp); }
/* Don't use these routes. Use websWriteHeaders, websEndHeaders instead. Write a webs header. This is a convenience routine to write a common header for a form back to the browser. */ PUBLIC void websHeader(Webs *wp) { assure(websValid(wp)); websWriteHeaders(wp, -1, 0); websWriteEndHeaders(wp); websWrite(wp, "<html>\n"); }
PUBLIC int process_web_write_response(Webs *wp, int code, char *buf, int size){ /* need route the code num in the future */ websSetStatus(wp, code); websWriteHeaders(wp, -1, 0); websWriteEndHeaders(wp); /* need check the size */ websWrite(wp, buf); websDone(wp); }
/* Legacy handler with old parameter sequence */ static int legacyTest(Webs *wp, char *prefix, char *dir, int flags) { websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteHeader(wp, "Content-Type", "text/plain"); websWriteEndHeaders(wp); websWrite(wp, "Hello Legacy World\n"); websDone(wp); return 1; }
static void writeCgiHeaders(Webs *wp, int status, ssize contentLength, char *location, char *contentType) { trace(5, "cgi: Start response headers"); websSetStatus(wp, status); websWriteHeaders(wp, contentLength, location); websWriteHeader(wp, "Pragma", "no-cache"); websWriteHeader(wp, "Cache-Control", "no-cache"); if (contentType) { websWriteHeader(wp, "Content-Type", contentType); } }
/* Implement /action/actionTest. Parse the form variables: name, address and echo back. */ static void actionTest(Webs *wp, char *path, char *query) { char *name, *address; name = websGetVar(wp, "name", NULL); address = websGetVar(wp, "address", NULL); websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteEndHeaders(wp); websWrite(wp, "<html><body><h2>name: %s, address: %s</h2></body></html>\n", name, address); websDone(wp); }
static void showTest(Webs *wp, char *path, char *query) { WebsKey *s; websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteEndHeaders(wp); websWrite(wp, "<html><body><pre>\n"); for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) { websWrite(wp, "%s=%s\n", s->name.value.string, s->content.value.string); } websWrite(wp, "</pre></body></html>\n"); websDone(wp); }
/* Generate a large response */ static int bigTest(int eid, Webs *wp, int argc, char **argv) { int i; websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteEndHeaders(wp); websWrite(wp, "<html>\n"); for (i = 0; i < 800; i++) { websWrite(wp, " Line: %05d %s", i, "aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccccddddddd<br/>\r\n"); } websWrite(wp, "</html>\n"); websDone(wp); return 0; }
static void sessionTest(Webs *wp, char *path, char *query) { char *number; if (scaselessmatch(wp->method, "POST")) { number = websGetVar(wp, "number", 0); websSetSessionVar(wp, "number", number); } else { number = websGetSessionVar(wp, "number", 0); } websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteEndHeaders(wp); websWrite(wp, "<html><body><p>Number %s</p></body></html>\n", number); websDone(wp); }
/* Serve static files */ static bool fileHandler(Webs *wp) { WebsFileInfo info; char *tmp, *date; ssize nchars; int code; assert(websValid(wp)); assert(wp->method); assert(wp->filename && wp->filename[0]); #if !ME_ROM if (smatch(wp->method, "DELETE")) { if (unlink(wp->filename) < 0) { websError(wp, HTTP_CODE_NOT_FOUND, "Cannot delete the URI"); } else { /* No content */ websResponse(wp, 204, 0); } } else if (smatch(wp->method, "PUT")) { /* Code is already set for us by processContent() */ websResponse(wp, wp->code, 0); } else #endif /* !ME_ROM */ { /* If the file is a directory, redirect using the nominated default page */ if (websPageIsDirectory(wp)) { nchars = strlen(wp->path); if (wp->path[nchars - 1] == '/' || wp->path[nchars - 1] == '\\') { wp->path[--nchars] = '\0'; } tmp = sfmt("%s/%s", wp->path, websIndex); websRedirect(wp, tmp); wfree(tmp); return 1; } if (websPageOpen(wp, O_RDONLY | O_BINARY, 0666) < 0) { #if ME_DEBUG if (wp->referrer) { trace(1, "From %s", wp->referrer); } #endif websError(wp, HTTP_CODE_NOT_FOUND, "Cannot open document for: %s", wp->path); return 1; } if (websPageStat(wp, &info) < 0) { websError(wp, HTTP_CODE_NOT_FOUND, "Cannot stat page for URL"); return 1; } code = 200; if (wp->since && info.mtime <= wp->since) { code = 304; info.size = 0; } websSetStatus(wp, code); websWriteHeaders(wp, info.size, 0); if ((date = websGetDateString(&info)) != NULL) { websWriteHeader(wp, "Last-Modified", "%s", date); wfree(date); } websWriteEndHeaders(wp); /* All done if the browser did a HEAD request */ if (smatch(wp->method, "HEAD")) { websDone(wp); return 1; } if (info.size > 0) { websSetBackgroundWriter(wp, fileWriteEvent); } else { websDone(wp); } } return 1; }
/* Process requests and expand all scripting commands. We read the entire web page into memory and then process. If you have really big documents, it is better to make them plain HTML files rather than Javascript web pages. */ static bool jstHandler(Webs *wp) { WebsFileInfo sbuf; char *token, *lang, *result, *ep, *cp, *buf, *nextp, *last; ssize len; int rc, jid; assert(websValid(wp)); assert(wp->filename && *wp->filename); assert(wp->ext && *wp->ext); buf = 0; if ((jid = jsOpenEngine(wp->vars, websJstFunctions)) < 0) { websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR, "Cannot create JavaScript engine"); goto done; } jsSetUserHandle(jid, wp); if (websPageStat(wp, &sbuf) < 0) { websError(wp, HTTP_CODE_NOT_FOUND, "Cannot stat %s", wp->filename); goto done; } if (websPageOpen(wp, O_RDONLY | O_BINARY, 0666) < 0) { websError(wp, HTTP_CODE_NOT_FOUND, "Cannot open URL: %s", wp->filename); goto done; } /* Create a buffer to hold the web page in-memory */ len = sbuf.size; if ((buf = walloc(len + 1)) == NULL) { websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR, "Cannot get memory"); goto done; } buf[len] = '\0'; if (websPageReadData(wp, buf, len) != len) { websError(wp, HTTP_CODE_NOT_FOUND, "Cannot read %s", wp->filename); goto done; } websPageClose(wp); websWriteHeaders(wp, (ssize) -1, 0); websWriteHeader(wp, "Pragma", "no-cache"); websWriteHeader(wp, "Cache-Control", "no-cache"); websWriteEndHeaders(wp); /* Scan for the next "<%" */ last = buf; for (rc = 0; rc == 0 && *last && ((nextp = strstr(last, "<%")) != NULL); ) { websWriteBlock(wp, last, (nextp - last)); nextp = skipWhite(nextp + 2); /* Decode the language */ token = "language"; if ((lang = strtokcmp(nextp, token)) != NULL) { if ((cp = strtokcmp(lang, "=javascript")) != NULL) { /* Ignore */; } else { cp = nextp; } nextp = cp; } /* Find tailing bracket and then evaluate the script */ if ((ep = strstr(nextp, "%>")) != NULL) { *ep = '\0'; last = ep + 2; nextp = skipWhite(nextp); /* Handle backquoted newlines */ for (cp = nextp; *cp; ) { if (*cp == '\\' && (cp[1] == '\r' || cp[1] == '\n')) { *cp++ = ' '; while (*cp == '\r' || *cp == '\n') { *cp++ = ' '; } } else { cp++; } } if (*nextp) { result = NULL; if (jsEval(jid, nextp, &result) == 0) { /* On an error, discard all output accumulated so far and store the error in the result buffer. Be careful if the user has called websError() already. */ rc = -1; if (websValid(wp)) { if (result) { websWrite(wp, "<h2><b>Javascript Error: %s</b></h2>\n", result); websWrite(wp, "<pre>%s</pre>", nextp); wfree(result); } else { websWrite(wp, "<h2><b>Javascript Error</b></h2>\n%s\n", nextp); } websWrite(wp, "</body></html>\n"); rc = 0; } goto done; } } } else { websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR, "Unterminated script in %s: \n", wp->filename); goto done; } } /* Output any trailing HTML page text */ if (last && *last && rc == 0) { websWriteBlock(wp, last, strlen(last)); } /* Common exit and cleanup */ done: if (websValid(wp)) { websPageClose(wp); if (jid >= 0) { jsCloseEngine(jid); } } websDone(wp); wfree(buf); return 1; }