Only one test is left to be squashed

This commit is contained in:
Ano-sys
2025-01-11 03:09:29 +01:00
parent 92581b3d4b
commit 4d6ea5c51f
4 changed files with 92 additions and 77 deletions
+68 -44
View File
@@ -54,13 +54,20 @@ neighbor check_neighborhood(uint16_t);
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);
elif(strstr(uri, static_)) uri_ptr += strlen(static_);
/*
char *it = (char *)uri;
char *it = (char*)uri;
// shift uri_ptr to last '/'
while(*it)
{
if (*it == '/') uri_ptr = it++;
if (*it == '/') uri_ptr = it;
it++;
}
@@ -94,7 +101,7 @@ void send_reply(int conn, struct request *request) {
// Find the resource with the given URI in the 'resources' array.
size_t resource_length;
// const char *resource = get(request->uri, resources, MAX_RESOURCES, &resource_length);
printf("Uri: %s\n", request->uri);
uint16_t resource_hash = hash_uri(request->uri);
fprintf(stderr, "Resource hash: %u\n", resource_hash);
@@ -120,50 +127,58 @@ void send_reply(int conn, struct request *request) {
}
else
{
// TODO: Success
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");
}
}
// DONE: Success
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(req->node_ip.s_addr);
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr.sin_addr, ip, INET_ADDRSTRLEN);
if (responsible == res) {
printf("Responsible - 404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
elif(responsible == res_303)
{
int suc_id = _SUCC_ID;
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);
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", ip, req->node_port, request->uri);
offset = strlen(reply);
printf("303\n");
}
else
}
else{
if (responsible == res) {
printf("Responsible - 404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
elif(responsible == res_303)
{
int suc_id = _SUCC_ID;
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)
{
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
offset = strlen(reply);
printf("503\n");
lookup_request lookreq;
lookup_request_init(&lookreq, 0, _NODE_ID, ntohl(inet_addr(_NODE_IP)), _NODE_PORT, resource_hash);
call_network(&lookreq, ntohl(inet_addr(_SUCC_IP)), _SUCC_PORT);
}
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)
{
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
offset = strlen(reply);
printf("503\n");
// TODO: Call other dht
lookup_request lookreq;
lookup_request_init(&lookreq, 0, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, resource_hash);
call_network(&lookreq, inet_addr(_SUCC_IP), _SUCC_PORT);
}
else {
printf("404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
} else if (strcmp(request->method, "PUT") == 0) {
// Try to set the requested resource with the given payload in the
// 'resources' array.
@@ -424,7 +439,6 @@ neighbor check_neighborhood(uint16_t hash) {
return res;
}
// TODO: check correctness
if ((pre_id == id && suc_id == id) || (id < pre_id && ((hash > pre_id && hash < 65535)) || hash <= id) || (hash > pre_id && hash <= id))
return res;
@@ -436,6 +450,7 @@ neighbor check_neighborhood(uint16_t hash) {
static void handle_udp_request(int _sock){
fprintf(stderr, "---%d---\n", _NODE_ID);
size_t received_bytes = 0;
lookup_request lr = {0};
@@ -446,10 +461,13 @@ static void handle_udp_request(int _sock){
perror("Func: handle_udp_request - failed to receive data from caller!");
exit(EXIT_FAILURE);
}
perror("---HANDLE_UDP---\n");
print_lookup_request(&lr);
extract_lookup_request(&lr);
print_lookup_request(&lr);
perror("----------------\n");
printf("Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
fprintf(stderr, "Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
// DO LOOKUP
if (lr.message_type == 0)
@@ -457,7 +475,7 @@ static void handle_udp_request(int _sock){
// Check belonging
const int suc_id = _SUCC_ID;
in_addr_t suc_ip = inet_addr(_SUCC_IP);
in_addr_t suc_ip = htonl(inet_addr(_SUCC_IP));
struct in_addr suc_ip_s;
suc_ip_s.s_addr = suc_ip;
@@ -484,7 +502,7 @@ static void handle_udp_request(int _sock){
*/
if (responsibility == not_res) {
// is succ responsible
if (lr.hash_id <= suc_id)
if (lr.hash_id <= suc_id || (suc_id < _NODE_ID && lr.hash_id <= 65535))
{
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
}
@@ -498,7 +516,7 @@ static void handle_udp_request(int _sock){
}
}
}
//call_network(&backfire, *callback_ip, callback_port);
call_network(&backfire, *callback_ip, callback_port);
}
elif (lr.message_type == 1)
{
@@ -506,6 +524,8 @@ static void handle_udp_request(int _sock){
clone_lookup_request(toLookup, &lr);
push_lookup(toLookup);
perror("PUSHED ");
print_lookup_request(toLookup);
}
else
{
@@ -526,7 +546,11 @@ int main(int argc, char **argv) {
}
init_lookup_table();
/*
lookup_request toLookup1;
lookup_request_init(&toLookup1, 1, 65535, ntohl(inet_addr("127.0.0.1")), 4710, 1);
push_lookup(&toLookup1);
*/
if(argc == 3) _NODE_ID = 0;
elif(argc == 4) _NODE_ID = atoi(argv[3]);