diff --git a/dht.c b/dht.c index 2fb11fc..64feb01 100644 --- a/dht.c +++ b/dht.c @@ -35,7 +35,7 @@ void extract_lookup_request(lookup_request* lr){ build_lookup_request(lr); } -int call_network(lookup_request* lr, in_addr_t ip, int port){ +int call_network(lookup_request* lr, in_addr_t ip, uint16_t port){ // int succ_sock; struct addrinfo hints, *servinfo; // *p; int rv; @@ -66,7 +66,7 @@ int call_network(lookup_request* lr, in_addr_t ip, int port){ freeaddrinfo(servinfo); lr->node_ip.s_addr = ntohl(addr.sin_addr.s_addr); - lr->node_port = ntohs(addr.sin_port); + // lr->node_port = ntohs(addr.sin_port); /* for(p = servinfo; p != NULL; p = p->ai_next) { diff --git a/dht.h b/dht.h index d9f9693..a1c426f 100644 --- a/dht.h +++ b/dht.h @@ -21,7 +21,7 @@ void build_lookup_request(lookup_request*); void extract_lookup_request(lookup_request*); -int call_network(lookup_request*, in_addr_t, int); +int call_network(lookup_request*, in_addr_t, uint16_t); void clone_lookup_request(lookup_request* dest, lookup_request* source); diff --git a/test/test_praxis2.py b/test/test_praxis2.py index 7c821a7..2170d6f 100644 --- a/test/test_praxis2.py +++ b/test/test_praxis2.py @@ -78,7 +78,7 @@ def test_listen(static_peer): @pytest.mark.timeout(1) -@pytest.mark.parametrize("uri", ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) +@pytest.mark.parametrize("uri", ['a', 'b', 'aa', 'ab', 'ac', 'ad', 'klm', 'xyz']) def test_immediate_dht(static_peer, uri): """Test hashing of request (1.2) Run peer in minimal (non-trivial) DHT @@ -121,7 +121,7 @@ def test_immediate_dht(static_peer, uri): @pytest.mark.timeout(1) -@pytest.mark.parametrize("uri", ['a', 'b']) +@pytest.mark.parametrize("uri", ['a', 'aa']) def test_lookup_sent(static_peer, uri): """Test for lookup to correct peer (1.3) @@ -219,7 +219,7 @@ def test_lookup_forward(static_peer): @pytest.mark.timeout(1) -@pytest.mark.parametrize("uri", ['a', 'b']) +@pytest.mark.parametrize("uri", ['a', 'aa']) def test_lookup_complete(static_peer, uri): """Test for correct lookup use (1.6) diff --git a/webserver.c b/webserver.c index 5447169..ecd210d 100644 --- a/webserver.c +++ b/webserver.c @@ -54,14 +54,23 @@ neighbor check_neighborhood(uint16_t); uint16_t hash_uri(const char *uri){ char *uri_ptr = (char*)uri; - char dynamic[] = "/dynamic/"; - char static_[] = "/static/"; + /* + char *it = (char *)uri; - if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic); - elif(strstr(uri, static_)) uri_ptr += strlen(static_); - elif(uri[0] == '/') uri_ptr++; + // shift uri_ptr to last '/' + while(*it) + { + if (*it == '/') uri_ptr = it++; + it++; + } - return pseudo_hash((unsigned char *)uri_ptr, strlen(uri_ptr)); + // skip last '/' + if (*uri_ptr == '/') uri_ptr++; + + if (!*uri_ptr) return -1; + */ + + return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr)); } /** @@ -87,6 +96,17 @@ void send_reply(int conn, struct request *request) { // const char *resource = get(request->uri, resources, MAX_RESOURCES, &resource_length); uint16_t resource_hash = hash_uri(request->uri); + + fprintf(stderr, "Resource hash: %u\n", resource_hash); + + if (resource_hash == -1) + { + printf("404\n"); + reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; + offset = strlen(reply); + goto direct_send; + } + neighbor responsible = check_neighborhood(resource_hash); lookup_request *req = find_lookup(resource_hash); @@ -108,25 +128,25 @@ void send_reply(int conn, struct request *request) { } if (responsible == res) { - sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); + printf("Responsible - 404\n"); + reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; offset = strlen(reply); - printf("200\n"); } elif(responsible == res_303) { int suc_id = _SUCC_ID; - if (resource_hash <= suc_id || suc_id >= 0 && suc_id < _NODE_ID) - { - printf("404\n"); - reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; - offset = strlen(reply); - } - else + if ((_NODE_ID < suc_id && resource_hash > _NODE_ID && resource_hash <= suc_id) || (_NODE_ID > suc_id && (resource_hash > _NODE_ID || resource_hash <= suc_id))) { sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", _SUCC_IP, _SUCC_PORT, request->uri); offset = strlen(reply); printf("303\n"); } + else + { + printf("404\n"); + reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; + offset = strlen(reply); + } } elif(responsible == not_res) { @@ -167,6 +187,8 @@ void send_reply(int conn, struct request *request) { offset = strlen(reply); } + direct_send: // ... do goto, yes you read that right ;) + // Send the reply back to the client if (send(conn, reply, offset, 0) == -1) { perror("send"); @@ -455,10 +477,12 @@ static void handle_udp_request(int _sock){ { const neighbor responsibility = check_neighborhood(lr.hash_id); + /* res is handles upfront by not res and suc responsibility check if (responsibility == res) { lookup_request_init(&backfire, 1, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, _PRED_ID); } - elif (responsibility == not_res) { + */ + if (responsibility == not_res) { // is succ responsible if (lr.hash_id <= suc_id) { @@ -466,14 +490,15 @@ static void handle_udp_request(int _sock){ } else { - lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID); + // lookup_request_init(&backfire, 0, suc_id, suc_ip, _SUCC_PORT, _NODE_ID); + clone_lookup_request(&backfire, &lr); callback_ip = &suc_ip_s.s_addr; callback_port = _SUCC_PORT; } } } - call_network(&backfire, *callback_ip, callback_port); + //call_network(&backfire, *callback_ip, callback_port); } elif (lr.message_type == 1) {