response *make_404_response() { char *path = "404.html", *html; response *resp = response_new(); strlist *filedata = strlist_new(); response_set_status_code(resp, 404); response_set_content_type(resp, "text/html"); html = "<html><head><title>Not Found</title></head><body> \ <h1>404!</h1><br>Sorry, the object you requested was not found. \ </body><html>"; filedata = readfile(path); if (filedata) { html = strlist_to_string(filedata); } //INFO("Read HTML:\n%s", html); response_set_body(resp, html); //INFO("Generated response:\n"); //IFINFO(response_write(resp, STDOUT_FILENO)); return resp; }
response *make_response(request *req) { strlist *filedata; char *html, *path = req->path; response *resp = response_new(); response_set_status_code(resp, 200); response_set_content_type(resp, "text/html"); if (req->method == GET || req->method == POST) { INFO("Requested path %s\n", path); if (path[0] == '/') path = &path[1]; filedata = readfile(path); if (!filedata) { WARN("Could not open file, generating 404...\n"); return make_404_response(); } html = strlist_to_string(filedata); response_set_body(resp, html); free(html); } return resp; }
static int grow_responses(nasth *s, unsigned short maxitems) { nast_response **tmp_resp; int i; if (s->nthreads >= maxitems) return 0; tmp_resp = realloc(s->responses, maxitems * sizeof(nast_response *)); if (tmp_resp == NULL) return -1; s->responses = tmp_resp; for (i = s->nthreads; i < maxitems; i++) { s->responses[i] = response_new(); if (s->responses[i] == NULL) return -1; } s->nthreads = maxitems; return 0; }
Response *dogHandler(Request *r) { return response_new(200, "WOOF!", 5); }
Response *testHandler(Request *r) { return response_new(200, "HELLO", 5); }
void _process_cmd(int fd, struct request *req) { char sent_buf[BUF_SIZE]; struct response *resp; memset(sent_buf, 0, BUF_SIZE); switch(req->cmd){ case CMD_PING:{ resp = response_new(0,OK_PONG); response_detch(resp,sent_buf); write(fd,sent_buf,strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_SET:{ struct slice sk, sv; if(req->argc == 3) { sk.len = req->argv[1]->len; sk.data = req->argv[1]->data; sv.len = req->argv[2]->len; sv.data = req->argv[2]->data; db_add(_svr.db, &sk, &sv); resp = response_new(0,OK); response_detch(resp,sent_buf); write(fd,sent_buf,strlen(sent_buf)); request_free_value(req); response_free(resp); break; } goto __default; } case CMD_MSET:{ int i; int c = req->argc; for (i = 1; i < c; i += 2) { struct slice sk, sv; sk.len = req->argv[i]->len; sk.data = req->argv[i]->data; sv.len = req->argv[i+1]->len; sv.data = req->argv[i+1]->data; db_add(_svr.db, &sk, &sv); } resp = response_new(0, OK); response_detch(resp, sent_buf); write(fd,sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_GET:{ int ret; struct slice sk; struct slice sv; if (req->argc == 2) { sk.len = req->argv[1]->len; sk.data = req->argv[1]->data; ret = db_get(_svr.db, &sk, &sv); if (ret == 1) { resp = response_new(1,OK_200); resp->argv[0] = sv.data; } else { resp = response_new(0,OK_404); resp->argv[0] = NULL; } response_detch(resp, sent_buf); write(fd,sent_buf,strlen(sent_buf)); request_free_value(req); response_free(resp); if (ret == 1) free(sv.data); break; } goto __default; } case CMD_MGET:{ int i; int ret; int c=req->argc; int sub_c=c-1; char **vals = calloc(c, sizeof(char*)); resp=response_new(sub_c, OK_200); for (i = 1; i < c; i++){ struct slice sk; struct slice sv; sk.len = req->argv[i]->len; sk.data = req->argv[i]->data; ret = db_get(_svr.db, &sk, &sv); if (ret == 1) vals[i-1] = sv.data; else vals[i-1] = NULL; resp->argv[i-1] = vals[i-1]; } response_detch(resp, sent_buf); write(fd, sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); for (i = 0; i < sub_c; i++){ if (vals[i]) free(vals[i]); } free(vals); break; } case CMD_INFO:{ char *infos; infos = db_info(_svr.db); resp = response_new(1, OK_200); resp->argv[0] = infos; response_detch(resp, sent_buf); write(fd,sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_DEL:{ int i; for (i = 1; i < req->argc; i++){ struct slice sk; sk.len = req->argv[i]->len; sk.data = req->argv[i]->data; db_remove(_svr.db, &sk); } resp = response_new(0, OK); response_detch(resp, sent_buf); write(fd, sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } case CMD_EXISTS:{ struct slice sk; sk.len = req->argv[1]->len; sk.data = req->argv[1]->data; int ret= db_exists(_svr.db, &sk); if(ret) write(fd,":1\r\n",4); else write(fd,":-1\r\n",5); } break; case CMD_SHUTDOWN: __ERROR("siloserver shutdown..."); db_close(_svr.db); exit(2); break; __default: default:{ resp = response_new(0, ERR); response_detch(resp, sent_buf); write(fd, sent_buf, strlen(sent_buf)); request_free_value(req); response_free(resp); break; } } }