void do_response(Request *req, Response ** res, sqlite3 *db) { if (!req->account) return; int id = -1; char *idStr = NULL; if((idStr = kvFindList(req->queryString, "id"))){ sscanf(idStr, "%d", &id); } Post *post = postGetById(db, id); if (!post) goto fail; likeDel(likeCreate(db, req->account->id, post->authorId, post->id)); if (kvFindList(req->queryString, "r")) { char sbuff[1024]; sprintf(sbuff, "/profile?id=%d", post->authorId); bsDel(idStr); *res = responseNewRedirect(sbuff); } fail: *res = responseNewRedirect("/dashboard"); }
Request *requestNew(char *buff) { Request *request = malloc(sizeof(Request)); char *segment, *bs; request->method = UNKNOWN_METHOD; request->path = NULL; request->uri = NULL; request->queryString = NULL; request->postBody = NULL; request->headers = NULL; request->cookies = NULL; request->account = NULL; // METHOD TOK(buff, " \t"); if (strcmp(segment, "OPTIONS") == 0) request->method = OPTIONS; else if (strcmp(segment, "GET") == 0) request->method = GET; else if (strcmp(segment, "HEAD") == 0) request->method = HEAD; else if (strcmp(segment, "POST") == 0) request->method = POST; else if (strcmp(segment, "PUT") == 0) request->method = PUT; else if (strcmp(segment, "DELETE") == 0) request->method = DELETE; else if (strcmp(segment, "TRACE") == 0) request->method = TRACE; else if (strcmp(segment, "CONNECT") == 0) request->method = CONNECT; else goto fail; // PATH TOK(NULL, " \t"); request->path = bsNew(segment); request->uri = bsNew(segment); if (strchr(request->path, '#') != NULL) goto fail; // VERSION TOK(NULL, "\n"); if (strncmp(segment, "HTTP/1.0", 8) != 0 && strncmp(segment, "HTTP/1.1", 8) != 0) goto fail; // HEADERS request->headers = parseHeaders(segment); // BODY bs = kvFindList(request->headers, "Content-Type"); if (bs != NULL && strncmp(bs, "application/x-www-form-urlencoded", 33) == 0) { segment = strtok(NULL, "\0"); if (segment == NULL) goto fail; request->postBody = parseQS(segment); } // QUERYSTRING segment = strchr(request->path, '?'); if (segment != NULL) { request->uri = bsNewLen(request->path, segment - request->path); request->queryString = parseQS(segment + 1); if (request->queryString == NULL) goto fail; } // COOKIES segment = kvFindList(request->headers, "Cookie"); if (segment != NULL) { request->cookies = parseCookies(segment); if (request->cookies == NULL) goto fail; } return request; fail: requestDel(request); return NULL; }