Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改一些字符串长度的bug #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions chap-30/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
add_executable(aio01 aio01.c)
target_link_libraries(aio01 yolanda)
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
target_link_libraries(aio01 rt)
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")
14 changes: 6 additions & 8 deletions chap-34/http_server01.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
//数据读到buffer之后的callback
int onRequest(struct http_request *httpRequest, struct http_response *httpResponse) {
char *url = httpRequest->url;
char *question = memmem(url, strlen(url), "?", 1);
char *path = NULL;
char *question = strstr(url, "?");
int url_len;
if (question != NULL) {
path = malloc(question - url);
strncpy(path, url, question - url);
url_len = question - url;
} else {
path = malloc(strlen(url));
strncpy(path, url, strlen(url));
url_len = strlen(url);
}

if (strcmp(path, "/") == 0) {
if (strncmp(url, "/", url_len) == 0) {
httpResponse->statusCode = OK;
httpResponse->statusMessage = "OK";
httpResponse->contentType = "text/html";
httpResponse->body = "<html><head><title>This is network programming</title></head><body><h1>Hello, network programming</h1></body></html>";
} else if (strcmp(path, "/network") == 0) {
} else if (strncmp(url, "/network", url_len) == 0) {
httpResponse->statusCode = OK;
httpResponse->statusMessage = "OK";
httpResponse->contentType = "text/plain";
Expand Down
1 change: 1 addition & 0 deletions lib/http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ int http_request_close_connection(struct http_request *httpRequest) {

if (httpRequest->version != NULL &&
strncmp(httpRequest->version, HTTP10, strlen(HTTP10)) == 0 &&
connection != NULL &&
strncmp(connection, KEEP_ALIVE, strlen(KEEP_ALIVE)) == 1) {
return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int process_status_line(char *start, char *end, struct http_request *httpRequest
int method_size = space - start;
httpRequest->method = malloc(method_size + 1);
strncpy(httpRequest->method, start, space - start);
httpRequest->method[method_size + 1] = '\0';
httpRequest->method[method_size] = '\0';

//url
start = space + 1;
Expand All @@ -27,13 +27,13 @@ int process_status_line(char *start, char *end, struct http_request *httpRequest
int url_size = space - start;
httpRequest->url = malloc(url_size + 1);
strncpy(httpRequest->url, start, space - start);
httpRequest->url[url_size + 1] = '\0';
httpRequest->url[url_size] = '\0';

//version
start = space + 1;
httpRequest->version = malloc(end - start + 1);
strncpy(httpRequest->version, start, end - start);
httpRequest->version[end - start + 1] = '\0';
httpRequest->version[end - start] = '\0';
assert(space != NULL);
return size;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/tcp_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ int handle_connection_closed(struct tcp_connection *tcpConnection) {
struct event_loop *eventLoop = tcpConnection->eventLoop;
struct channel *channel = tcpConnection->channel;
event_loop_remove_channel_event(eventLoop, channel->fd, channel);
if(close(channel->fd) < 0) {
yolanda_msgx("close fd %d failed, errno : %d", channel->fd, errno);
}
if (tcpConnection->connectionClosedCallBack != NULL) {
tcpConnection->connectionClosedCallBack(tcpConnection);
}
Expand Down