add null check http_parse

This commit is contained in:
2025-01-28 01:01:20 +01:00
parent 3da61ef47c
commit aebecfb4c2
3 changed files with 24 additions and 8 deletions

View File

@@ -1,12 +1,13 @@
# 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
- [meson](https://mesonbuild.com/index.html)
- [doxygen](https://www.doxygen.nl/)
- Optional, just to build the docs
- Optional, just to build the docs.
## How to build
@@ -21,7 +22,8 @@ And then run `cws`!
## Docs
```bash
$ git submodule update --init # inside the cws directory
# inside the cws directory
$ git submodule update --init
$ doxygen
```
@@ -29,7 +31,7 @@ And then open the `docs/html/index.html`.
## Roadmap
- [x] Understading basic web server concepts
- [x] Understanding basic web server concepts
- [x] Basic server
- [ ] CLI args
- [ ] 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/)
You can find my journey inside the `notes` directory or on
my [blog](https://francescorocca.me/building-an-http-server-in-c/).
You can find my journey inside the `notes` directory.

View File

@@ -13,22 +13,27 @@ http_t *http_parse(char *request_str, int sockfd) {
return NULL;
}
/* Insert sockfd */
/* Insert socket file descriptor */
request->sockfd = sockfd;
/* Parse HTTP method */
char *pch = strtok(request_str, " ");
if (pch == NULL) {
return NULL;
}
printf("[http] method: %s\n", pch);
http_parse_method(request, pch);
/* Parse location */
pch = strtok(NULL, " ");
if (pch == NULL) {
return NULL;
}
printf("[http] location: %s\n", pch);
strncpy(request->location, pch, LOCATION_LEN);
/* Parse location path */
/* TODO: Prevent Path Traversal */
/* TODO: Fix warnings */
if (strcmp(request->location, "/") == 0) {
snprintf(request->location_path, LOCATION_LEN, "%s/index.html", WWW);
} else {
@@ -38,6 +43,9 @@ http_t *http_parse(char *request_str, int sockfd) {
/* Parse HTTP version */
pch = strtok(NULL, " \r\n");
if (pch == NULL) {
return NULL;
}
printf("[http] version: %s\n", pch);
strncpy(request->http_version, pch, HTTP_VERSION_LEN);

View File

@@ -125,6 +125,13 @@ void handle_clients(int sockfd) {
/* Parse HTTP request */
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);
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
close_client(epfd, client_fd, clients);