additional error handling
- fix http_parse_request to recognize too short lines - fix wrong error handling in send_response_handler - return not found instead of not implemented in handle_api - fixed bug when printing out peer information in socket.c
This commit is contained in:
parent
0ca6b6282e
commit
36f9faebfe
@ -5,7 +5,7 @@ DEP_LIB_DIR=$(abspath $(DEP_BASE_DIR)/lib)
|
|||||||
CFLAGS=-g -pthread -std=gnu11 -Wall -Werror -Wmissing-prototypes -I$(DEP_INCLUDE_DIR)
|
CFLAGS=-g -pthread -std=gnu11 -Wall -Werror -Wmissing-prototypes -I$(DEP_INCLUDE_DIR)
|
||||||
|
|
||||||
# include lib directory into runtime path to facilitate dynamic linking
|
# include lib directory into runtime path to facilitate dynamic linking
|
||||||
LDFLAGS=-Wl,-rpath -Wl,$(DEP_LIB_DIR)
|
LDFLAGS=-pthread -Wl,-rpath -Wl,$(DEP_LIB_DIR)
|
||||||
LDLIBS=-L$(DEP_LIB_DIR) -ljwt -ljansson -lcrypto -ldl
|
LDLIBS=-L$(DEP_LIB_DIR) -ljwt -ljansson -lcrypto -ldl
|
||||||
|
|
||||||
HEADERS=socket.h http.h hexdump.h buffer.h bufio.h
|
HEADERS=socket.h http.h hexdump.h buffer.h bufio.h
|
||||||
|
23
src/http.c
23
src/http.c
@ -39,7 +39,7 @@ http_parse_request(struct http_transaction *ta)
|
|||||||
{
|
{
|
||||||
size_t req_offset;
|
size_t req_offset;
|
||||||
ssize_t len = bufio_readline(ta->client->bufio, &req_offset);
|
ssize_t len = bufio_readline(ta->client->bufio, &req_offset);
|
||||||
if (len <= 0)
|
if (len < 2) // error, EOF, or less than 2 characters
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
char *request = bufio_offset2ptr(ta->client->bufio, req_offset);
|
char *request = bufio_offset2ptr(ta->client->bufio, req_offset);
|
||||||
@ -181,7 +181,7 @@ start_response(struct http_transaction * ta, buffer_t *res)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Send response headers to client */
|
/* Send response headers to client */
|
||||||
static int
|
static bool
|
||||||
send_response_header(struct http_transaction *ta)
|
send_response_header(struct http_transaction *ta)
|
||||||
{
|
{
|
||||||
buffer_t response;
|
buffer_t response;
|
||||||
@ -189,14 +189,14 @@ send_response_header(struct http_transaction *ta)
|
|||||||
|
|
||||||
start_response(ta, &response);
|
start_response(ta, &response);
|
||||||
if (bufio_sendbuffer(ta->client->bufio, &response) == -1)
|
if (bufio_sendbuffer(ta->client->bufio, &response) == -1)
|
||||||
return -1;
|
return false;
|
||||||
|
|
||||||
buffer_appends(&ta->resp_headers, CRLF);
|
buffer_appends(&ta->resp_headers, CRLF);
|
||||||
if (bufio_sendbuffer(ta->client->bufio, &ta->resp_headers) == -1)
|
if (bufio_sendbuffer(ta->client->bufio, &ta->resp_headers) == -1)
|
||||||
return -1;
|
return false;
|
||||||
|
|
||||||
buffer_delete(&response);
|
buffer_delete(&response);
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send a full response to client with the content in resp_body. */
|
/* Send a full response to client with the content in resp_body. */
|
||||||
@ -206,7 +206,7 @@ send_response(struct http_transaction *ta)
|
|||||||
// add content-length. All other headers must have already been set.
|
// add content-length. All other headers must have already been set.
|
||||||
add_content_length(&ta->resp_headers, ta->resp_body.len);
|
add_content_length(&ta->resp_headers, ta->resp_body.len);
|
||||||
|
|
||||||
if (send_response_header(ta) == -1)
|
if (!send_response_header(ta))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return bufio_sendbuffer(ta->client->bufio, &ta->resp_body) != -1;
|
return bufio_sendbuffer(ta->client->bufio, &ta->resp_body) != -1;
|
||||||
@ -297,21 +297,20 @@ handle_static_asset(struct http_transaction *ta, char *basedir)
|
|||||||
add_content_length(&ta->resp_headers, st.st_size);
|
add_content_length(&ta->resp_headers, st.st_size);
|
||||||
http_add_header(&ta->resp_headers, "Content-Type", "%s", guess_mime_type(fname));
|
http_add_header(&ta->resp_headers, "Content-Type", "%s", guess_mime_type(fname));
|
||||||
|
|
||||||
rc = send_response_header(ta);
|
bool success = send_response_header(ta);
|
||||||
if (rc == -1)
|
if (!success)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
rc = bufio_sendfile(ta->client->bufio, filefd, NULL, st.st_size);
|
success = bufio_sendfile(ta->client->bufio, filefd, NULL, st.st_size) == st.st_size;
|
||||||
out:
|
out:
|
||||||
close(filefd);
|
close(filefd);
|
||||||
return rc;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
handle_api(struct http_transaction *ta)
|
handle_api(struct http_transaction *ta)
|
||||||
{
|
{
|
||||||
return send_error(ta, HTTP_NOT_IMPLEMENTED, "API not implemented");
|
return send_error(ta, HTTP_NOT_FOUND, "API not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set up an http client, associating it with a bufio buffer. */
|
/* Set up an http client, associating it with a bufio buffer. */
|
||||||
|
@ -121,11 +121,11 @@ socket_accept_client(int accepting_socket)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The following will debug with debugging your server.
|
/* The following will help with debugging your server.
|
||||||
* Adjust as necessary.
|
* Adjust and/or remove as you see fit.
|
||||||
*/
|
*/
|
||||||
char peer_addr[1024], peer_port[10];
|
char peer_addr[1024], peer_port[10];
|
||||||
int rc = getnameinfo(&peer, sizeof peer,
|
int rc = getnameinfo(&peer, peersize,
|
||||||
peer_addr, sizeof peer_addr, peer_port, sizeof peer_port,
|
peer_addr, sizeof peer_addr, peer_port, sizeof peer_port,
|
||||||
NI_NUMERICHOST | NI_NUMERICSERV);
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user