jf-framework
Loading...
Searching...
No Matches
http.h
Go to the documentation of this file.
1
6
7#ifndef JF_FRAMEWORK_HTTP_H
8#define JF_FRAMEWORK_HTTP_H
9
10#include <export.h>
11#include <stddef.h>
12#include <stdint.h>
13
14typedef enum {
15 HTTP_CONTINUE = 100,
16 HTTP_SWITCHING_PROTOCOLS = 101,
17 HTTP_PROCESSING = 102,
18
19 HTTP_OK = 200,
20 HTTP_CREATED = 201,
21 HTTP_ACCEPTED = 202,
22 HTTP_NO_CONTENT = 204,
23 HTTP_PARTIAL_CONTENT = 206,
24
25 HTTP_MULTIPLE_CHOICES = 300,
26 HTTP_MOVED_PERMANENTLY = 301,
27 HTTP_FOUND = 302,
28 HTTP_SEE_OTHER = 303,
29 HTTP_NOT_MODIFIED = 304,
30 HTTP_TEMPORARY_REDIRECT = 307,
31 HTTP_PERMANENT_REDIRECT = 308,
32
33 HTTP_BAD_REQUEST = 400,
34 HTTP_UNAUTHORIZED = 401,
35 HTTP_FORBIDDEN = 403,
36 HTTP_NOT_FOUND = 404,
37 HTTP_METHOD_NOT_ALLOWED = 405,
38 HTTP_REQUEST_TIMEOUT = 408,
39 HTTP_CONFLICT = 409,
40 HTTP_GONE = 410,
41 HTTP_LENGTH_REQUIRED = 411,
42 HTTP_PAYLOAD_TOO_LARGE = 413,
43 HTTP_UNSUPPORTED_MEDIA_TYPE = 415,
44 HTTP_TOO_MANY_REQUESTS = 429,
45
46 HTTP_INTERNAL_SERVER_ERROR = 500,
47 HTTP_NOT_IMPLEMENTED = 501,
48 HTTP_BAD_GATEWAY = 502,
49 HTTP_SERVICE_UNAVAILABLE = 503,
50 HTTP_GATEWAY_TIMEOUT = 504,
51 HTTP_VERSION_NOT_SUPPORTED = 505
52} HttpStatus;
53
54// https://www.rfc-editor.org/info/rfc2616/
55typedef struct {
56 char method[8];
57 char path[256];
58 char version[16];
59 char query[256];
60 char host[256];
61 char contentType[128];
62 size_t contentLength;
63 struct {
64 char name[64];
65 char value[256];
66 } headers[32];
67 uint32_t headerCount;
68 const char* body;
69 size_t bodyLength;
70 struct {
71 char key[32];
72 char value[128];
73 } params[32];
74 uint32_t paramCount;
76
77typedef struct {
78 HttpStatus response;
79 char reason[64];
80 char contentType[128];
81 size_t contentLength;
82 struct {
83 char name[64];
84 char value[256];
85 } headers[32];
86 uint32_t headerCount;
87 char* body;
88 size_t bodyCapacity;
90
91uint32_t jf_ParseHttpRequest(HttpRequest* httpRequest, const char* buffer, size_t length);
92void jf_InitHttpResponse(HttpResponse* response);
93uint32_t jf_SendHttpResponse(int32_t sockfd, const HttpResponse* response);
94JF_EXPORT void jf_Send(HttpResponse* httpResponse, const char* contentType, const char* body, HttpStatus status);
95JF_EXPORT void jf_SendString(HttpResponse* httpResponse, const char* string, HttpStatus status);
96JF_EXPORT void jf_SendHTMLFile(HttpResponse* httpResponse, const char* path, HttpStatus status);
97JF_EXPORT void jf_SendHTML(HttpResponse* httpResponse, const char* html, HttpStatus status);
98JF_EXPORT void jf_SendJson(HttpResponse* httpResponse, const char* json, HttpStatus status);
99JF_EXPORT void jf_Redirect(HttpResponse* httpResponse, const char* destination, HttpStatus status);
100JF_EXPORT const char* jf_GetParameter(const HttpRequest* httpRequest, const char* key);
101JF_EXPORT uint32_t jf_AddHeader(HttpResponse* httpResponse, const char* name, const char* value);
102
103#endif
void jf_SendHTMLFile(HttpResponse *httpResponse, const char *path, HttpStatus status)
Wrapper for jf_Send.
Definition http.c:210
void jf_SendJson(HttpResponse *httpResponse, const char *json, HttpStatus status)
Wrapper for jf_Send.
Definition http.c:262
void jf_SendHTML(HttpResponse *httpResponse, const char *html, HttpStatus status)
Wrapper for jf_Send.
Definition http.c:253
uint32_t jf_AddHeader(HttpResponse *httpResponse, const char *name, const char *value)
Set HTTP response headers.
Definition http.c:283
const char * jf_GetParameter(const HttpRequest *httpRequest, const char *key)
Retreives parameter from http request.
Definition http.c:305
void jf_Send(HttpResponse *httpResponse, const char *contentType, const char *body, HttpStatus status)
Sends HTTP response.
Definition http.c:184
void jf_Redirect(HttpResponse *httpResponse, const char *destination, HttpStatus status)
Wrapper for jf_Send.
Definition http.c:271
void jf_SendString(HttpResponse *httpResponse, const char *string, HttpStatus status)
Wrapper for jf_Send.
Definition http.c:201
void jf_InitHttpResponse(HttpResponse *response)
Initializes response struct.
Definition http.c:118
uint32_t jf_SendHttpResponse(int32_t sockfd, const HttpResponse *response)
Sends HTTP response.
Definition http.c:139
uint32_t jf_ParseHttpRequest(HttpRequest *httpRequest, const char *buffer, size_t length)
Parses the HTTP request.
Definition http.c:26
Definition http.h:55
Definition http.h:77