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; }
int main() { /* toggle_bit */ ok(7 == toggle_bit(0b0101, 1)); ok(0b0100 == toggle_bit(0b0101, 0)); ok(0b0111 == toggle_bit(0b1111, 3)); /* split_url */ { char *name, *suffix; name = malloc(256*sizeof(char)); ok(0 == split_url(&name, &suffix, "github.com")); ok(0 == strcmp(name, "github")); ok(0 == strcmp(suffix, "com")); ok(0 != split_url(&name, &suffix, "www.github.com")); free(name); } /* is_valid_str */ ok(is_valid_str("github", 6)); ok(!is_valid_str("github.com", 10)); ok(is_valid_str("g1thub", 6)); /* bitsquat_char */ { char c = 'a'; ok('a' != bitsquat_char(c)); } /* random_loc */ { size_t loc = random_loc("github"); ok(loc > 0 && loc < 6); } /* create_url */ { struct Url m_url; m_url = create_url("github.com"); ok(0 == strcmp(m_url.name, "github")); ok(0 == strcmp(m_url.suffix, "com")); free_url(m_url); } /* generate_entries and bitsquat_entries */ { struct BSentries bs_entries; struct Url url = create_url("github.com"); bs_entries = generate_entries(url, 3); ok(3 == bs_entries.n_names); ok(0 == strcmp(bs_entries.suffix, "com")); ok(0 == strcmp(bs_entries.names[0], "github")); bitsquat_entries(bs_entries); ok(0 != strcmp(bs_entries.names[0], "github")); free_url(url); free_bs_entries(bs_entries); } return 0; }