/* * Sample submit.cgi function */ int submit(HttpState* state) { int i; if(state->length) { /* buffer to write out */ if(state->offset < state->length) { state->offset += sock_fastwrite(&state->s, state->buffer + (int)state->offset, (int)state->length - (int)state->offset); } else { state->offset = 0; state->length = 0; } } else { switch(state->substate) { case 0: _f_strcpy(state->buffer, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n"); break; case 1: /* init the FORMSpec data */ FORMSpec[0].value[0] = '\0'; FORMSpec[1].value[0] = '\0'; state->p = state->buffer; parse_post(state); state->substate++; return 0; case 2: http_setcookie(state->buffer, FORMSpec[0].value); break; case 3: _f_strcpy(state->buffer, "\r\n\r\n<html><head><title>Results</title></head><body>\r\n"); break; case 4: sprintf(state->buffer, "<p>Username: %s<p>\r\n<p>Email: %s<p>\r\n", FORMSpec[0].value, FORMSpec[1].value); break; case 5: _f_strcpy(state->buffer, "<p>Go <a href=\"/\">home</a></body></html>\r\n"); break; default: state->substate = 0; return 1; } state->length = strlen(state->buffer); state->offset = 0; state->substate++; } return 0; }
void blizzard::http::process() { bool quit = false; int res = 0; while (!quit) { switch (state_) { case sUndefined: want_read = true; want_write = false; state_ = sReadingHead; break; case sReadingHead: res = parse_title(); if (res > 0) { state_ = sDone; quit = true; } else if (res < 0) { quit = true; } break; case sReadingHeaders: res = parse_header_line(); if (res > 0) { state_ = sDone; quit = true; } else if (res < 0) { quit = true; } if (state_ == sReadyToHandle) { quit = true; } break; case sReadingPost: res = parse_post(); if (res < 0) { quit = true; } break; case sReadyToHandle: commit(); state_ = sWriting; break; case sWriting: want_write = true; res = write_data(); if (res < 0) { quit = true; } break; case sDone: quit = true; break; default: break; } } }
int main() { while (FCGI_Accept() >= 0) { char type[80] = "html"; char entry[10] = "0"; char comments[6] = "false"; char *req_method; char gets[1000]; req_method = getenv("REQUEST_METHOD"); if (strcmp(req_method, "PUT") == 0) { char id[10]; strcpy(gets, getenv("QUERY_STRING")); if (getenv("QUERY_STRING") == NULL) { printf("Status: 400 Bad Request\r\n\r\n"); } else { get_param(gets, "id", id, 10); int entry_id = strtol(id, NULL, 10); // grab the post data int len = strtol(getenv("CONTENT_LENGTH"), NULL, 10); char *content = (char *)malloc(sizeof(char) * len); fread(content, sizeof(char), len, stdin); LIBXML_TEST_VERSION char *t; char *c; const int TS = 256; int code; t = (char *)malloc(sizeof(char) * TS); c = (char *)malloc(sizeof(char) * len); if (parse_post(content, t, TS, c, len) == 0) { code = update_entry(entry_id, t, c); if (code == 0) { printf("Status: 200 OK\r\n"); printf("Content-type: text/xml; charset=UTF-8\r\n" "\r\n"); generate_entries(1, entry_id, "atom_single.ct"); } else { printf("Status: 500 Internal Server Error\r\n\r\nDB Error Code: %d - %d \n\n", code, entry_id); } } else { printf("Status: 400 Bad Request\r\n\r\n"); } free(t); free(c); free(content); } } else if (strcmp(req_method, "DELETE") == 0) { char id[10]; strcpy(gets, getenv("QUERY_STRING")); if (getenv("QUERY_STRING") == NULL) { printf("Status: 400 Bad Request\r\n\r\n"); } else { get_param(gets, "id", id, 10); if (remove_entry(strtol(id, NULL, 10)) == 0) { printf("Status: 204 No Content\r\n\r\n"); } else { printf("Status: 400 Bad Request\r\n\r\n"); } } } else if (strcmp(req_method, "POST") == 0) { if (getenv("QUERY_STRING") == NULL) { printf("Status: 400 Bad Request\r\n\r\n"); } // parse the post function char function[20]; strcpy(gets, getenv("QUERY_STRING")); get_param(gets, "function", function, 20); get_param(gets, "format", type, 20); // grab the post data int len = strtol(getenv("CONTENT_LENGTH"), NULL, 10); char *content = (char *)malloc(sizeof(char) * len); fread(content, sizeof(char), len, stdin); //atom post! if (strcmp(type, "atom") == 0) { LIBXML_TEST_VERSION char *t; char *c; const int TS = 256; int id; t = (char *)malloc(sizeof(char) * TS); c = (char *)malloc(sizeof(char) * len); if (parse_post(content, t, TS, c, len) == 0) { id = add_entry(t, c); if (id > 0) { printf("Status: 201 Created\r\n\r\n"); printf("Content-type: text/xml; charset=UTF-8\r\n" "\r\n"); generate_entries(1, id, "atom_single.ct"); } else { printf("Status: 500 Internal Server Error\r\n\r\nDB Error Code: %d", id); } } else { printf("Status: 400 Bad Request\r\n\r\n"); } free(t); free(c); free(content); } else if (strcmp(function, "comment") == 0) { char entry_id[10]; char comment[len]; int ac; get_param(content, "entry_id", entry_id, 10); get_param(content, "content", comment, len); ac = add_comment(strtol(entry_id, NULL, 10), comment); fprintf(stderr, "Does it get here?"); if (ac > 0) { printf("Content-type: application/json; charset=UTF-8\r\n"); printf("\r\n"); printf("{'success': true}"); } else { printf("Status: 500 Internal Server Error\r\n\r\n"); printf("{'success': false, 'error_code': %d}", ac); } } else { printf("Status: 400 Bad Request\r\n\r\n"); } free(content); /* int len = strtol(getenv("CONTENT_LENGTH"), NULL, 10); char *content = (char *)malloc(sizeof(char) * len); fread(content, sizeof(char), len, stdin); */ } else // end if( type == "POST") if (strcmp(req_method, "GET") == 0) { if (getenv("QUERY_STRING") != NULL) { char gets[1000]; strcpy(gets, getenv("QUERY_STRING")); get_param(gets, "entry", entry, 10); strcpy(gets, getenv("QUERY_STRING")); get_param(gets, "format", type, 20); strcpy(gets, getenv("QUERY_STRING")); get_param(gets, "get_comments", comments, 6); } if (strcmp(type, "html") == 0) { // application/xhtml+xml doesn't work with IE -_-;; //printf("Content-type: application/xhtml+xml; charset=UTF-8\r\n" // have to serve xhtml as html... IE sucks printf("Content-type: text/html; charset=UTF-8\r\n" "\r\n"); } else if (strcmp(type, "json") == 0) { printf("Content-type: application/json; charset=UTF-8\r\n" "\r\n"); } else if (strcmp(type, "rss") == 0) { printf("Content-type: application/rss+xml; charset=UTF-8\r\n" "\r\n"); } else if (strcmp(type, "atom") == 0) { printf("Content-type: application/atom+xml; charset=UTF-8\r\n" "\r\n"); } else { printf("Status: 400 Bad Request\r\n\r\n"); break; } if (strcmp(comments, "true") == 0) { strcat(type, "_comments.ct"); generate_comments(strtol(entry, NULL, 10), type); } else { /* defaults to creating the main blog */ strcat(type, ".ct"); if(strcmp(entry,"0") == 0) { generate_entries(10, 1, type); } else { generate_entries(1, strtol(entry, NULL, 10), type); } } } // end if( type == "GET") } return 0; }