add null check http_parse
This commit is contained in:
13
README.md
13
README.md
@@ -1,12 +1,13 @@
|
|||||||
# cws
|
# cws
|
||||||
|
|
||||||
A simple Web Server written in C (learning purposes), it works only on Linux systems.
|
A minimal web server written in C. This is a personal project; it is not intended to be a production-ready tool, nor
|
||||||
|
will it ever be. Use it at your own risk.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- [meson](https://mesonbuild.com/index.html)
|
- [meson](https://mesonbuild.com/index.html)
|
||||||
- [doxygen](https://www.doxygen.nl/)
|
- [doxygen](https://www.doxygen.nl/)
|
||||||
- Optional, just to build the docs
|
- Optional, just to build the docs.
|
||||||
|
|
||||||
## How to build
|
## How to build
|
||||||
|
|
||||||
@@ -21,7 +22,8 @@ And then run `cws`!
|
|||||||
## Docs
|
## Docs
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ git submodule update --init # inside the cws directory
|
# inside the cws directory
|
||||||
|
$ git submodule update --init
|
||||||
$ doxygen
|
$ doxygen
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -29,7 +31,7 @@ And then open the `docs/html/index.html`.
|
|||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
||||||
- [x] Understading basic web server concepts
|
- [x] Understanding basic web server concepts
|
||||||
- [x] Basic server
|
- [x] Basic server
|
||||||
- [ ] CLI args
|
- [ ] CLI args
|
||||||
- [ ] Enhance web server
|
- [ ] Enhance web server
|
||||||
@@ -47,5 +49,4 @@ And then open the `docs/html/index.html`.
|
|||||||
|
|
||||||
- [Beej's Guide to Network Programming](https://beej.us/guide/bgnet/)
|
- [Beej's Guide to Network Programming](https://beej.us/guide/bgnet/)
|
||||||
|
|
||||||
You can find my journey inside the `notes` directory or on
|
You can find my journey inside the `notes` directory.
|
||||||
my [blog](https://francescorocca.me/building-an-http-server-in-c/).
|
|
||||||
|
|||||||
@@ -13,22 +13,27 @@ http_t *http_parse(char *request_str, int sockfd) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Insert sockfd */
|
/* Insert socket file descriptor */
|
||||||
request->sockfd = sockfd;
|
request->sockfd = sockfd;
|
||||||
|
|
||||||
/* Parse HTTP method */
|
/* Parse HTTP method */
|
||||||
char *pch = strtok(request_str, " ");
|
char *pch = strtok(request_str, " ");
|
||||||
|
if (pch == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
printf("[http] method: %s\n", pch);
|
printf("[http] method: %s\n", pch);
|
||||||
http_parse_method(request, pch);
|
http_parse_method(request, pch);
|
||||||
|
|
||||||
/* Parse location */
|
/* Parse location */
|
||||||
pch = strtok(NULL, " ");
|
pch = strtok(NULL, " ");
|
||||||
|
if (pch == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
printf("[http] location: %s\n", pch);
|
printf("[http] location: %s\n", pch);
|
||||||
strncpy(request->location, pch, LOCATION_LEN);
|
strncpy(request->location, pch, LOCATION_LEN);
|
||||||
|
|
||||||
/* Parse location path */
|
/* Parse location path */
|
||||||
/* TODO: Prevent Path Traversal */
|
/* TODO: Prevent Path Traversal */
|
||||||
/* TODO: Fix warnings */
|
|
||||||
if (strcmp(request->location, "/") == 0) {
|
if (strcmp(request->location, "/") == 0) {
|
||||||
snprintf(request->location_path, LOCATION_LEN, "%s/index.html", WWW);
|
snprintf(request->location_path, LOCATION_LEN, "%s/index.html", WWW);
|
||||||
} else {
|
} else {
|
||||||
@@ -38,6 +43,9 @@ http_t *http_parse(char *request_str, int sockfd) {
|
|||||||
|
|
||||||
/* Parse HTTP version */
|
/* Parse HTTP version */
|
||||||
pch = strtok(NULL, " \r\n");
|
pch = strtok(NULL, " \r\n");
|
||||||
|
if (pch == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
printf("[http] version: %s\n", pch);
|
printf("[http] version: %s\n", pch);
|
||||||
strncpy(request->http_version, pch, HTTP_VERSION_LEN);
|
strncpy(request->http_version, pch, HTTP_VERSION_LEN);
|
||||||
|
|
||||||
|
|||||||
@@ -125,6 +125,13 @@ void handle_clients(int sockfd) {
|
|||||||
|
|
||||||
/* Parse HTTP request */
|
/* Parse HTTP request */
|
||||||
http_t *request = http_parse(data, client_fd);
|
http_t *request = http_parse(data, client_fd);
|
||||||
|
|
||||||
|
if (request == NULL) {
|
||||||
|
close_client(epfd, client_fd, clients);
|
||||||
|
http_free(request);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
http_send_response(request);
|
http_send_response(request);
|
||||||
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
|
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
|
||||||
close_client(epfd, client_fd, clients);
|
close_client(epfd, client_fd, clients);
|
||||||
|
|||||||
Reference in New Issue
Block a user