HttpServer
Micro http library written in c [UNIX]
http_server.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2024 Lorenzo Chesi
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #ifndef _HTTP_SERVER_H_
26 #define _HTTP_SERVER_H_
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <http_parser.h>
33 
34 #ifdef __linux__
35 #include <sys/socket.h>
36 #include <sys/select.h>
37 #else
38 #include <sockLib.h>
39 #include <hostLib.h>
40 #include <selectLib.h>
41 #endif
42 #include <arpa/inet.h>
43 #include <sys/types.h>
44 #include <netinet/in.h>
45 #include <stdbool.h>
46 #include <pthread.h>
47 
58 /* GLOBAL DEFINITION *****************************************************************/
59 
60 #ifndef HTTP_MAX_HANDLERS
65 #define HTTP_MAX_HANDLERS 128
66 #endif
67 
68 #ifndef HTTP_MAX_WORKERS
73 #define HTTP_MAX_WORKERS 4
74 #endif
75 
76 /* UTILITY FUNCTIONS ************************************************************************/
77 
86 #define IMPORT_FILE(file, sym) \
87 __asm__( \
88  ".section \".rodata\" \n" \
89  ".global " #sym "\n" \
90  ".balign 16\n" \
91  #sym":\n" \
92  ".incbin \"" file "\"\n" \
93  #sym "_end:\n" \
94  ".balign 16\n" \
95  ".global _sizeof_" #sym "\n" \
96  "_sizeof_" #sym":\n" \
97  ".long " #sym "_end - " #sym "\n" \
98  ".section \".text\" \n" \
99 ); \
100 extern __attribute__((aligned(16))) const size_t _sizeof_ ## sym; \
101 extern __attribute__((aligned(16))) __attribute__((nonstring)) const char sym[]
102 
113 int send_http_response(int socket, enum http_status status, const char* header, const char* content, size_t content_lenght);
114 
115 /* HTTP SERVER TYPES ***************************************************************************/
116 
125 typedef int(*HttpCallback)(int socket, void* data);
126 
128 struct HttpHandler {
132  enum http_method method;
134  const char* url;
136  HttpCallback callback;
138  void* data;
139 };
140 
142 enum HttpRequestState {
146  HTTP_WORKER_REQUEST_EMPTY,
148  HTTP_WORKER_REQUEST_READY,
150  HTTP_WORKER_REQUEST_RUNNING
151 };
152 
166 };
167 
169 struct HttpRequest {
173  int socket;
175  enum HttpRequestState state;
176  union {
178  const struct HttpHandler* handlers;
180  const struct HttpHandler* handler;
181  } ptr;
182 };
183 
184 
185 /* HTTP SERVER FUNCTIONS ***********************************************************************************/
186 
191 struct HttpServer {
192  /* READ-ONLY */
193 
195  enum HttpServerState state;
196 
197  /* PRIVATE */
198 
200  pthread_t _thread;
202  int _listener;
204  struct sockaddr_in _addr;
206  fd_set _master_set;
208  struct HttpHandler _handlers[HTTP_MAX_HANDLERS];
210  pthread_t _workers[HTTP_MAX_WORKERS];
212  struct HttpRequest _requests[HTTP_MAX_WORKERS];
214  pthread_mutex_t _mutex_sync;
216  pthread_cond_t _cond_sync;
217 };
218 
228 int http_server_init(struct HttpServer* this, const char address[], uint16_t port);
229 
241 int http_server_add_handler(struct HttpServer* this, enum http_method method, const char* url, HttpCallback callback, void* data);
242 
250 int http_server_start(struct HttpServer* this);
251 
259 int http_server_stop(struct HttpServer* this);
260 
268 int http_server_join(struct HttpServer* this);
269 
270 #ifdef __cplusplus
271 }
272 #endif
273 #endif
int(* HttpCallback)(int socket, void *data)
Funzione di callback.
Definition: http_server.h:125
HttpServerState
Stati del server.
Definition: http_server.h:157
#define HTTP_MAX_HANDLERS
Numero massimo di handlers.
Definition: http_server.h:65
int http_server_init(struct HttpServer *this, const char address[], uint16_t port)
Inizializza la struttura Server.
Definition: http_server.c:123
int http_server_stop(struct HttpServer *this)
Termina forzatamente il server.
Definition: http_server.c:174
int http_server_start(struct HttpServer *this)
Avvia il server.
Definition: http_server.c:574
#define HTTP_MAX_WORKERS
Numero massimo di worker.
Definition: http_server.h:73
int http_server_add_handler(struct HttpServer *this, enum http_method method, const char *url, HttpCallback callback, void *data)
Aggiunge un handler al server.
Definition: http_server.c:644
int http_server_join(struct HttpServer *this)
Attende che il server sia terminato.
Definition: http_server.c:629
@ HTTP_SERVER_INITIALIZED
Server inizializzato.
Definition: http_server.h:161
@ HTTP_SERVER_RUNNING
Server in esecuzione.
Definition: http_server.h:163
@ HTTP_SERVER_STOPPING
Server in spegnimento.
Definition: http_server.h:165
@ HTTP_SERVER_STOPPED
Server fermo.
Definition: http_server.h:159
int send_http_response(int socket, enum http_status status, const char *header, const char *content, size_t content_lenght)
Invia una risposta HTTP/1.1.
Definition: http_server.c:50
Struttura utilizzata per memorizzare tutti gli handler.
Definition: http_server.h:128
Struttura per il contesto della richiesta.
Definition: http_server.h:169
const struct HttpHandler * handlers
lista degli handler
Definition: http_server.h:178
const struct HttpHandler * handler
handler che ha fatto match
Definition: http_server.h:180
Struttura del server.
Definition: http_server.h:191
enum HttpServerState state
Indica lo stato del server.
Definition: http_server.h:195