-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
141 lines (119 loc) · 4.26 KB
/
server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef SERVER_H
#define SERVER_H
#include <stdio.h>
// gcc -c server.c -o server.o -lws2_32 && gcc -shared -o server.dll server.o -lws2_32
// in linux gcc -c server.c && ar rcs libserver.a server.o
#ifdef _WIN32
#pragma comment(lib, "ws2_32.lib") // Link with ws2_32.lib
#define InitializeWinsock WSADATA wsa; if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("WSAStartup failed\n"); return 1; }
#define ClearWinsock WSACleanup()
#else
#define closesocket close
#define InitializeWinsock
#define SOCKET int
#define ClearWinsock
#define min(a, b) (a>b)?b:a
#endif
#define BUFFER_SIZE 4096
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
#define RESET "\x1B[0m"
// TODO remove all unnecessary from .h and add them to .c so while importing it don't import _func's
typedef struct _server server;
typedef struct _str str;
typedef struct _list list;
typedef void (*Callback)(server *, str *response, list *headers, str *content_buffer);
struct _server_node{
char* method;
char* path;
Callback callback;
struct _server_node* next;
};
struct _server{
struct _server_node *head;
struct _server_node *tail;
int length;
int debug;
int _stop;
// void (*print)(struct _str*); // print method
void (*route)(struct _server*, char*, char*, Callback callback);
int (*run)(struct _server*, char*, int, int);
void (*stop)(struct _server *);
// char* (*get)(struct _list*, char*);
};
struct _server http_server();
int find(char *s, char *value);
int findchar(char *s, char value);
size_t get_file_size(FILE *fptr);
void send_file(str *response, char *filename);
void send_file_with_header(str *response, char *filename, char *header);
// Function to convert a request string to an integer representation
int _server_request_to_int(const char *str);
void _server_route(struct _server *self, char *method, char *path, Callback callback);
int _server_run(struct _server *self, char *host, int port, int debug);
void _server_stop(struct _server *self);
void _server_debug(struct _server *self, const char *, ...);
// String()
struct _str_chunk{
char *data;
int length;
struct _str_chunk *next;
};
struct _str{
struct _str_chunk *head;
struct _str_chunk *tail;
int length;
void (*print)(struct _str*); // print method
void (*append_format)(struct _str*, const char*, ...);
void (*append)(struct _str*, char*); // Append object to the end of the string.
void (*append_byte)(struct _str*, char*, int);
void (*free)(struct _str*); // Free the string.
void (*clear)(struct _str*); // Clear the string
void (*raw)(struct _str*, char*); // copy the string to the buffer
void (*get_chunk)(struct _str*, int, char*, int);
};
void _str_data__repr__(char* );
void _str_print(struct _str *);
void _str_append_format(struct _str *, const char *, ...);
void _str_append(struct _str *, char *);
void _str_append_byte(struct _str *, char *, int);
void _str_raw(struct _str *, char *);
void _str_clear(struct _str *);
void _str_get_chunk(struct _str *, int , char *, int );
struct _str String();
// List()
typedef struct _list_data{
char *key;
char *value;
} Header;
struct _list_node{
struct _list_data *data;
struct _list_node *next;
};
struct _list{
struct _list_node *head;
struct _list_node *tail;
int length;
void (*print)(struct _list*); // print method
void (*append)(struct _list*, struct _list_data*); // Append object to the end of the list.
void (*free)(struct _list*); // Free the list.
char* (*get)(struct _list*, char*);
};
// custom functions
char* _list_get_header(struct _list*, char*);
// utility functions
void _list_data__repr__(struct _list_data*);
struct _list_data * _list_data_copy(struct _list_data *);
void _list_data_free(struct _list_data *);
void _list_print(struct _list*);
void _list_append(struct _list*, struct _list_data*);
void _list_remove(struct _list*, struct _list_data*);
void _list_index(struct _list*, struct _list_data*);
void _list_free(struct _list*);
struct _list List();
#endif