void wiki_page_forbiden(HttpResponse *res) { http_response_printf(res, "<html>\n<p>Permission denied - You need to login.</p>" "<a href='javascript:javascript:history.go(-1)'>Return to the previous page.</a>\n" "</html>\n"); http_response_set_status(res, 403, "Forbidden</"); http_response_send(res); exit(0); }
int wiki_redirect(HttpResponse *res, char *location) { int header_len = strlen(location) + 14; char *header = alloca(sizeof(char)*header_len); snprintf(header, header_len, "Location: %s\r\n", location); http_response_append_header(res, header); http_response_printf(res, "<html>\n<p>Redirect to %s</p>\n</html>\n", location); http_response_set_status(res, 302, "Moved Temporarily"); http_response_send(res); exit(0); }
/* ** Send a reply indicating that the HTTP request was malformed */ static void malformed_request(int code, char *info) { HttpResponse *res = NULL; res = http_response_new(NULL); http_response_set_status(res, 501, "Not Implemented"); http_response_printf(res, "<html><body>Unrecognized HTTP Request Code=%i</body></html>\n", code); http_response_send(res); /* log Error */ syslog(LOG_LOCAL0|LOG_INFO, "Malformed request 501\nCode=%i\n%s\n", code,info); exit(0); }
void webserver_send_response(ClientSocket* client, enum EHttpStatus status, const char* body, const char* content_type) { // Build the Content-Length string char content_length[20] = {0}; snprintf(content_length, 20, "%zu", (body ? strlen(body) : 0)); if (!content_type) { content_type = "text/plain"; } // Build the response object HttpResponse* res = http_response_new(); http_response_set_status(res, HTTP_VERSION_1_0, status); http_response_add_header(res, "Server", "webserver"); http_response_add_header(res, "Content-Length", content_length); if (body) { http_response_add_header(res, "Content-Type", content_type); } if (body) { http_response_set_body(res, body); } // Send the response and clean up client_socket_send(client, http_response_string(res), http_response_length(res)); http_response_free(res); }
void wiki_handle_rest_call(HttpRequest *req, HttpResponse *res, char *func) { if (func != NULL && *func != '\0') { if (!strcmp(func, "page/get")) { char *page = http_request_param_get(req, "page"); if (page == NULL) page = http_request_get_query_string(req); if (page && (access(page, R_OK) == 0)) { http_response_printf(res, "%s", file_read(page)); http_response_send(res); return; } } else if (!strcmp(func, "page/set")) { char *wikitext = NULL, *page = NULL; if( ( (wikitext = http_request_param_get(req, "text")) != NULL) && ( (page = http_request_param_get(req, "page")) != NULL)) { file_write(page, wikitext); http_response_printf(res, "success"); http_response_send(res); return; } } else if (!strcmp(func, "page/delete")) { char *page = http_request_param_get(req, "page"); if (page == NULL) page = http_request_get_query_string(req); if (page && (unlink(page) > 0)) { http_response_printf(res, "success"); http_response_send(res); return; } } else if (!strcmp(func, "page/exists")) { char *page = http_request_param_get(req, "page"); if (page == NULL) page = http_request_get_query_string(req); if (page && (access(page, R_OK) == 0)) { http_response_printf(res, "success"); http_response_send(res); return; } } else if (!strcmp(func, "pages") || !strcmp(func, "search")) { WikiPageList **pages = NULL; int n_pages, i; char *expr = http_request_param_get(req, "expr"); if (expr == NULL) expr = http_request_get_query_string(req); pages = wiki_get_pages(&n_pages, expr); if (pages) { for (i=0; i<n_pages; i++) { struct tm *pTm; char datebuf[64]; pTm = localtime(&pages[i]->mtime); strftime(datebuf, sizeof(datebuf), "%Y-%m-%d %H:%M", pTm); http_response_printf(res, "%s\t%s\n", pages[i]->name, datebuf); } http_response_send(res); return; } } } http_response_set_status(res, 500, "Error"); http_response_printf(res, "<html><body>Failed</body></html>\n"); http_response_send(res); return; }
void wiki_handle_http_request(HttpRequest *req) { HttpResponse *res = http_response_new(req); char *page = http_request_get_path_info(req); char *command = http_request_get_query_string(req); char *wikitext = ""; util_dehttpize(page); /* remove any encoding on the requested page name. */ if (!strcmp(page, "/")) { if (access("WikiHome", R_OK) != 0) wiki_redirect(res, "/WikiHome?create"); page = "/WikiHome"; } if (!strcmp(page, "/styles.css")) { /* Return CSS page */ http_response_set_content_type(res, "text/css"); http_response_printf(res, "%s", CssData); http_response_send(res); exit(0); } if (!strcmp(page, "/favicon.ico")) { /* Return favicon */ http_response_set_content_type(res, "image/ico"); http_response_set_data(res, FaviconData, FaviconDataLen); http_response_send(res); exit(0); } page = page + 1; /* skip slash */ if (!strncmp(page, "api/", 4)) { char *p; page += 4; for (p=page; *p != '\0'; p++) if (*p=='?') { *p ='\0'; break; } wiki_handle_rest_call(req, res, page); exit(0); } /* A little safety. issue a malformed request for any paths, * There shouldn't need to be any.. */ if (strchr(page, '/')) { http_response_set_status(res, 404, "Not Found"); http_response_printf(res, "<html><body>404 Not Found</body></html>\n"); http_response_send(res); exit(0); } if (!strcmp(page, "Changes")) { wiki_show_changes_page(res); } else if (!strcmp(page, "ChangesRss")) { wiki_show_changes_page_rss(res); } else if (!strcmp(page, "Search")) { wiki_show_search_results_page(res, http_request_param_get(req, "expr")); } else if (!strcmp(page, "Create")) { if ( (wikitext = http_request_param_get(req, "title")) != NULL) { /* create page and redirect */ wiki_redirect(res, http_request_param_get(req, "title")); } else { /* show create page form */ wiki_show_create_page(res); } } else { /* TODO: dont blindly write wikitext data to disk */ if ( (wikitext = http_request_param_get(req, "wikitext")) != NULL) { file_write(page, wikitext); } if (access(page, R_OK) == 0) /* page exists */ { wikitext = file_read(page); if (!strcmp(command, "edit")) { /* print edit page */ wiki_show_edit_page(res, wikitext, page); } else { wiki_show_page(res, wikitext, page); } } else { if (!strcmp(command, "create")) { wiki_show_edit_page(res, NULL, page); } else { char buf[1024]; snprintf(buf, 1024, "%s?create", page); wiki_redirect(res, buf); } } } }