95/95git add http.c socket.c!

This commit is contained in:
Felicia Seo 2022-12-11 18:51:41 -05:00
parent 00151f2e18
commit 152cd33d83
2 changed files with 77 additions and 5 deletions

View File

@ -123,12 +123,19 @@ http_process_headers(struct http_transaction *ta)
* are zero-terminated strings. * are zero-terminated strings.
*/ */
if (!strcasecmp(field_name, "Cookie")) { if (!strcasecmp(field_name, "Cookie")) {
if ((field_value == NULL) || (strlen(field_value) <= 11))
char* cookie2;
char* cookie1 = strtok_r(field_value, ";", &cookie2);
cookie2++;
ta->extra_cookie = cookie2;
if ((cookie1 == NULL) || (strlen(cookie1) <= 11))
{ {
ta->resp_status = HTTP_PERMISSION_DENIED; ta->resp_status = HTTP_PERMISSION_DENIED;
return false; return false;
} }
ta->token = field_value + 11; // + 11 gets rid of "auth_token=" heading. ta->token = cookie1 + 11; // + 11 gets rid of "auth_token=" heading.
ta->valid_token = validate_token_exp(ta, ta->token); ta->valid_token = validate_token_exp(ta, ta->token);
} }
@ -664,7 +671,21 @@ handle_private(struct http_transaction *ta)
ta->resp_status = HTTP_OK; ta->resp_status = HTTP_OK;
return handle_static_asset(ta, server_root); return handle_static_asset(ta, server_root);
} }
return send_error(ta, HTTP_PERMISSION_DENIED, "invalid token");
if ((ta->extra_cookie == NULL) || (strlen(ta->extra_cookie) <= 11)) {
return send_error(ta, HTTP_PERMISSION_DENIED, "???");
}
ta->token = ta->extra_cookie + 11; // + 11 gets rid of "auth_token=" heading.
ta->valid_token = validate_token_exp(ta, ta->token);
if (ta->valid_token) {
ta->resp_status = HTTP_OK;
return handle_static_asset(ta, server_root);
}
return send_error(ta, HTTP_PERMISSION_DENIED, "???");
} }
/* Handle a single HTTP transaction. Returns true on success. */ /* Handle a single HTTP transaction. Returns true on success. */

View File

@ -54,6 +54,7 @@ socket_open_bind_listen(char * port_number_string, int backlog)
} }
char printed_addr[1024]; char printed_addr[1024];
for (pinfo = info; pinfo; pinfo = pinfo->ai_next) { for (pinfo = info; pinfo; pinfo = pinfo->ai_next) {
assert (pinfo->ai_protocol == IPPROTO_TCP); assert (pinfo->ai_protocol == IPPROTO_TCP);
int rc = getnameinfo(pinfo->ai_addr, pinfo->ai_addrlen, int rc = getnameinfo(pinfo->ai_addr, pinfo->ai_addrlen,
@ -73,8 +74,58 @@ socket_open_bind_listen(char * port_number_string, int backlog)
/* Skip any non-IPv4 addresses. /* Skip any non-IPv4 addresses.
* Adding support for protocol independence/IPv6 is part of the project. * Adding support for protocol independence/IPv6 is part of the project.
*/ */
//if (pinfo->ai_family != AF_INET) if (pinfo->ai_family != AF_INET6)
//continue; continue;
int s = socket(pinfo->ai_family, pinfo->ai_socktype, pinfo->ai_protocol);
if (s == -1) {
perror("socket");
return -1;
}
// See https://stackoverflow.com/a/3233022 for a good explanation of what this does
int opt = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
rc = bind(s, pinfo->ai_addr, pinfo->ai_addrlen);
if (rc == -1) {
perror("bind");
close(s);
return -1;
}
rc = listen(s, backlog);
if (rc == -1) {
perror("listen");
close(s);
return -1;
}
freeaddrinfo(info);
return s;
}
for (pinfo = info; pinfo; pinfo = pinfo->ai_next) {
assert (pinfo->ai_protocol == IPPROTO_TCP);
int rc = getnameinfo(pinfo->ai_addr, pinfo->ai_addrlen,
printed_addr, sizeof printed_addr, NULL, 0,
NI_NUMERICHOST);
if (rc != 0) {
fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(rc));
return -1;
}
/* Uncomment this to see the address returned
printf("%s: %s\n", pinfo->ai_family == AF_INET ? "AF_INET" :
pinfo->ai_family == AF_INET6 ? "AF_INET6" : "?",
printed_addr);
*/
/* Skip any non-IPv4 addresses.
* Adding support for protocol independence/IPv6 is part of the project.
*/
if (pinfo->ai_family != AF_INET)
continue;
int s = socket(pinfo->ai_family, pinfo->ai_socktype, pinfo->ai_protocol); int s = socket(pinfo->ai_family, pinfo->ai_socktype, pinfo->ai_protocol);
if (s == -1) { if (s == -1) {