Skip to content

Commit

Permalink
Small fixes and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
64 committed Jan 2, 2018
1 parent 0dfc929 commit 5495363
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ static int finalise_request(struct client *client, struct stream *stream, bool *
strcpy(pathbuf, "data/static");
strcat(pathbuf, stream->req.pathbuf); // Won't overflow since we terminated it at REQ_MAX_PATH - 1

log_debug("GET %s", pathbuf + strlen("data/static/") - 1);
//log_debug("GET %s", pathbuf + strlen("data/static/") - 1);
stream->req.pathptr = &pathbuf[0];
// Prevent directory traversal attacks
if (strstr(stream->req.pathbuf, "../") != NULL) {
Expand Down Expand Up @@ -888,7 +888,7 @@ static int do_read(struct client *client) {
rv = -1;
goto loop_end;
} else {
if (parse_frame(client, recv_buffer, nread) < 0) {
if (client->state != HH_GOAWAY && parse_frame(client, recv_buffer, nread) < 0) {
rv = -1;
goto loop_end;
}
Expand Down Expand Up @@ -1052,9 +1052,12 @@ int client_on_data_received(struct client *client) {
if (do_read(client) < 0)
goto graceful_exit;
break;
case HH_GOAWAY:
case HH_GOAWAY: {
// TODO: Check s2n_recv for EOF
case HH_TLS_SHUTDOWN:
if (do_read(client) < 0)
goto graceful_exit;
break;
} case HH_TLS_SHUTDOWN:
// Ignore it
break;
default:
Expand Down
2 changes: 2 additions & 0 deletions src/hh.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ static int load_server_cert(void) {
return -1;
}

// TODO: Don't leak on early return
// Read certificate file into buffer
fseek(cert_file, 0, SEEK_END);
long cert_size = ftell(cert_file);
Expand Down Expand Up @@ -426,6 +427,7 @@ static int server_listen(int server_fd, unsigned short port) {
state->epoll_fd = -1;
if ((errno = pthread_create(&worker_threads[i], NULL, worker_event_loop, state)) != 0) {
log_fatal("Call to pthread_create failed (%s)", strerror(errno));
free(event_fds);
return -1;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ void request_send_headers(struct client *client, struct stream *stream) {
//__builtin_unreachable();
}

if (stream->id == 1) {
// This is the first request the client is sending
HEADER("strict-transport-security", "max-age=31536000");
}

assert(pos < MAX_HEADER_FIELDS);

if (end_stream)
Expand Down Expand Up @@ -108,11 +113,9 @@ int request_fulfill(struct stream *s, uint8_t *buf, size_t *max_size) {
// Will automagically go to HH_STREAM_CLOSED if already in HH_STREAM_HCLOSED_REMOTE
stream_change_state(s, HH_STREAM_HCLOSED_LOCAL);
flags |= HH_END_STREAM;
int rv = close(s->req.fd);
s->req.state = HH_REQ_DONE;
if (rv != 0) {
if (close(s->req.fd) != 0)
log_warn("Call to close failed (%s)", strerror(errno));
}
s->req.fd = -1;
} else if (nwritten < 0) {
log_warn("Call to read failed (%s)", strerror(errno));
Expand Down
11 changes: 7 additions & 4 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ void stream_add_child(struct stream *stream, struct stream *child) {
stream->children = child;
} else {
// TODO: Make this O(1) since it's a bit of a bottleneck
struct stream *start = stream->children;
while (start->siblings != NULL)
start = start->siblings;
start->siblings = child;
struct stream *start = child->children;
if (start != NULL) {
while (start->siblings != NULL)
start = start->siblings;
start->siblings = stream->siblings;
}
stream->siblings = child;
}
}

Expand Down

0 comments on commit 5495363

Please sign in to comment.