Skip to content

Add easy ability to set status codes and format response body#5

Open
Kiyoshika wants to merge 3 commits intoinfraredCoding:masterfrom
Kiyoshika:feat/status-codes
Open

Add easy ability to set status codes and format response body#5
Kiyoshika wants to merge 3 commits intoinfraredCoding:masterfrom
Kiyoshika:feat/status-codes

Conversation

@Kiyoshika
Copy link

I did this PR independent of my other PR, #4 since I didn't know which one would be merged first (if any) to avoid complicating things. This is a rebase of the master branch (at the time).

I modified the HTTP_Server struct a bit to hold the status code and the response body. This comes with two new functions:

Set the the status code using any of the pre-defined enums (listed below)

void http_set_status_code(
    HTTP_Server* http_server, 
    const enum http_status_code_e);

// example
http_set_status_code(&http_server, OK);

Prepare the response body before sending to the client (see main.c for the full example usage). This will automatically prepend the status code for us so we don't have to manually strncat it ourselves.

void http_set_response_body(
    HTTP_Server* http_server,
    const char* body);

As of now the following status codes are supported:

200 OK
201 CREATED
400 Bad request
401 Unauthorized
403 Forbidden
404 Not found
500 Internal Error
501 Not implemented

I didn't add all of them, just what I feel like are probably the most common ones (it's easy enough to change in the future).

See the attached video sample where I tested the OK and NOT_FOUND status codes after slight modifications to the main.c example.

cerveur.status.codes.1.mp4

@Kiyoshika Kiyoshika changed the title Feat/status codes Add easy ability to set status codes and format response body Jan 23, 2023
After using the address sanitizer (-fsanitize=address) I noticed that the lack of null terminator on the response body was causing problems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant