diff --git a/src/connection.c b/src/connection.c index 5264981..2363e0b 100644 --- a/src/connection.c +++ b/src/connection.c @@ -217,9 +217,12 @@ int connection_do_read(connection_t *conn) { int ssl_error = SSL_get_error(conn->ssl, bytes_read); if (ssl_error == SSL_ERROR_WANT_READ || ssl_error == SSL_ERROR_WANT_WRITE) { errno = EAGAIN; + return -1; + } + if (bytes_read == 0) { return 0; } - return bytes_read; + return -1; } } else { bytes_read = read(conn->fd, buf->data + buf->tail, available); @@ -514,8 +517,11 @@ static void handle_client_read(connection_t *conn) { clock_gettime(CLOCK_MONOTONIC, &ts); conn->request_start_time = ts.tv_sec + ts.tv_nsec / 1e9; - if (strncmp(conn->request.uri, "/rproxy/dashboard", 17) == 0 || - strncmp(conn->request.uri, "/rproxy/api/stats", 17) == 0) { + #define DASHBOARD_PATH "/rproxy/dashboard" + #define STATS_PATH "/rproxy/api/stats" + + if (strncmp(conn->request.uri, DASHBOARD_PATH, sizeof(DASHBOARD_PATH) - 1) == 0 || + strncmp(conn->request.uri, STATS_PATH, sizeof(STATS_PATH) - 1) == 0) { log_info("[ROUTING-INTERNAL] Serving internal route %s for fd=%d", conn->request.uri, conn->fd); @@ -526,7 +532,7 @@ static void handle_client_read(connection_t *conn) { } conn->state = CLIENT_STATE_SERVING_INTERNAL; - if (strncmp(conn->request.uri, "/rproxy/dashboard", 17) == 0) { + if (strncmp(conn->request.uri, DASHBOARD_PATH, sizeof(DASHBOARD_PATH) - 1) == 0) { dashboard_serve(conn); } else { dashboard_serve_stats_api(conn); @@ -536,15 +542,13 @@ static void handle_client_read(connection_t *conn) { if (!conn->request.keep_alive) { conn->state = CLIENT_STATE_CLOSING; - return; } - - memset(&conn->request, 0, sizeof(http_request_t)); - conn->request.keep_alive = 1; - conn->state = CLIENT_STATE_READING_HEADERS; - continue; + return; } + #undef DASHBOARD_PATH + #undef STATS_PATH + log_info("[ROUTING-FORWARD] Forwarding request for fd=%d: %s %s", conn->fd, conn->request.method, conn->request.uri); @@ -729,11 +733,17 @@ void connection_handle_event(struct epoll_event *event) { connection_accept(fd); } } else { + int handshake_just_completed = 0; if (conn->type == CONN_TYPE_UPSTREAM && conn->ssl && !conn->ssl_handshake_done) { handle_ssl_handshake(conn); if (!conn->ssl_handshake_done) { return; } + handshake_just_completed = 1; + } + + if (handshake_just_completed && buffer_available_read(&conn->write_buf) > 0) { + connection_do_write(conn); } if (event->events & EPOLLOUT) {