/* Receives an HTTP response from socket SOCKFD; returns a decoded KVMessage. * Returns NULL if there is an error. */ kvresponse_t *kvresponse_recieve(int sockfd) { kvresponse_t *kvres = calloc(1, sizeof(kvresponse_t)); struct http_response *res = http_response_parse(sockfd); if (!res) goto error; kvres->type = kvresponse_get_status_code(res->status); if (kvres->type == EMPTY) goto error; kvres->body = res->body; free(res); return kvres; error: http_response_free(res); kvresponse_free(kvres); return NULL; }
/* Receives an HTTP response from socket SOCKFD and decodes into KVRES. * Returns false if there is an error. */ bool kvresponse_receive(kvresponse_t *kvres, int sockfd) { bool success = false; http_response_t res; success = http_response_receive(&res, sockfd); if (!success) goto error; kvres->type = kvresponse_get_status_code(res.status); if (kvres->type == EMPTY) goto error; strcpy(kvres->body, res.body); return true; error: return false; }