diff --git a/webserver.c b/webserver.c index 0bf105a..bb2f74a 100644 --- a/webserver.c +++ b/webserver.c @@ -52,12 +52,13 @@ typedef enum neighbor{ uint16_t hash_uri(const char *uri){ char *uri_ptr = (char*)uri; + /* char dynamic[] = "/dynamic/"; char static_[] = "/static/"; if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic) + 1; elif(strstr(uri, static_)) uri_ptr += strlen(static_) + 1; - + */ /* char *it = (char*)uri; @@ -77,22 +78,38 @@ uint16_t hash_uri(const char *uri){ return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr)); } -neighbor check_neighborhood(uint16_t hash) { - int pre_id = _PRED_ID; - int id = _NODE_ID; - int suc_id = _SUCC_ID; +#include +#include - if(hash > id && hash <= suc_id) return res_303; - - if ((pre_id == id && suc_id == id) || (id < pre_id && ((hash > pre_id && hash < 65535)) || hash <= id) || (hash > pre_id && hash <= id)) - return res; - - if ((id < suc_id && hash > id && hash <= suc_id) || (hash > id || hash <= suc_id)) - return not_res; - - return dunno; +static inline bool in_range(uint16_t me, uint16_t suc, uint16_t hash) { + if (me < suc) { + return (hash > me && hash <= suc); + } + elif (me > suc) { + return (hash > me || hash <= suc); + } + else { + return false; + } } +neighbor check_neighborhood(uint16_t hash) { + uint16_t pred_id = _PRED_ID; + uint16_t id = _NODE_ID; + uint16_t suc_id = _SUCC_ID; + + if (in_range(id, suc_id, hash)) { + return res_303; + } + else if (in_range(pred_id, id, hash)) { + return res; + } + else { + return not_res; + } +} + + /** * Sends an HTTP reply to the client based on the received request. * @@ -163,10 +180,12 @@ void send_reply(int conn, struct request *request) { const char* resource = get(request->uri, resources, MAX_RESOURCES, &resource_length); if (resource) { + perror("Sending 200\n"); size_t payload_offset = sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n", resource_length); memcpy(reply + payload_offset, resource, resource_length); offset = payload_offset + resource_length; } else { + perror("Sending 404\n"); reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; offset = strlen(reply); } @@ -175,22 +194,27 @@ void send_reply(int conn, struct request *request) { // 'resources' array. if (set(request->uri, request->payload, request->payload_length, resources, MAX_RESOURCES)) { + perror("Sending 204\n"); reply = "HTTP/1.1 204 No Content\r\n\r\n"; offset = strlen(reply); } else { + perror("Sending 201\n"); reply = "HTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n"; offset = strlen(reply); } } else if (strcmp(request->method, "DELETE") == 0) { // Try to delete the requested resource from the 'resources' array if (delete (request->uri, resources, MAX_RESOURCES)) { + perror("Sending 205\n"); reply = "HTTP/1.1 204 No Content\r\n\r\n"; offset = strlen(reply); } else { + perror("Sending 404 - DELETE\n"); reply = "HTTP/1.1 404 Not Found\r\n\r\n"; offset = strlen(reply); } } else { + perror("Sending 501\n"); reply = "HTTP/1.1 501 Method Not Supported\r\n\r\n"; offset = strlen(reply); }