@velox0/cerver 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,8 @@
2
2
  * http_parser.c — Minimal HTTP/1.1 request parser.
3
3
  *
4
4
  * Parses method, path, query string, and headers from a raw HTTP request.
5
- * No external dependencies.
5
+ * All parsing is done IN-PLACE on the caller's buffer — no copies are made.
6
+ * The caller must keep the buffer alive for the lifetime of the request.
6
7
  */
7
8
 
8
9
  #include "cerver.h"
@@ -17,179 +18,182 @@
17
18
  /* ------------------------------------------------------------------ */
18
19
 
19
20
  static int hex_val(char c) {
20
- if (c >= '0' && c <= '9') return c - '0';
21
- if (c >= 'a' && c <= 'f') return c - 'a' + 10;
22
- if (c >= 'A' && c <= 'F') return c - 'A' + 10;
23
- return -1;
21
+ if (c >= '0' && c <= '9') return c - '0';
22
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
23
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
24
+ return -1;
24
25
  }
25
26
 
26
- static void url_decode(char *str) {
27
- char *src = str;
28
- char *dst = str;
29
-
30
- while (*src) {
31
- if (*src == '%' && src[1] && src[2]) {
32
- int hi = hex_val(src[1]);
33
- int lo = hex_val(src[2]);
34
- if (hi >= 0 && lo >= 0) {
35
- *dst++ = (char)((hi << 4) | lo);
36
- src += 3;
37
- continue;
38
- }
39
- }
40
- if (*src == '+') {
41
- *dst++ = ' ';
42
- src++;
43
- continue;
44
- }
45
- *dst++ = *src++;
27
+ static void url_decode(char* str) {
28
+ char* src = str;
29
+ char* dst = str;
30
+
31
+ while (*src) {
32
+ if (*src == '%' && src[1] && src[2]) {
33
+ int hi = hex_val(src[1]);
34
+ int lo = hex_val(src[2]);
35
+ if (hi >= 0 && lo >= 0) {
36
+ *dst++ = (char)((hi << 4) | lo);
37
+ src += 3;
38
+ continue;
39
+ }
40
+ }
41
+ if (*src == '+') {
42
+ *dst++ = ' ';
43
+ src++;
44
+ continue;
46
45
  }
47
- *dst = '\0';
46
+ *dst++ = *src++;
47
+ }
48
+ *dst = '\0';
48
49
  }
49
50
 
50
51
  /* ------------------------------------------------------------------ */
51
- /* Parse query string: "a=1&b=2" → key-value pairs */
52
+ /* Parse query string IN-PLACE: "a=1&b=2" → key-value pairs */
52
53
  /* ------------------------------------------------------------------ */
53
54
 
54
- static void parse_query_string(char *qs, cerver_request_t *req) {
55
- if (!qs || !*qs) return;
56
-
57
- char *saveptr = NULL;
58
- char *pair = strtok_r(qs, "&", &saveptr);
59
-
60
- while (pair && req->query_count < CERVER_MAX_QUERY) {
61
- char *eq = strchr(pair, '=');
62
- if (eq) {
63
- *eq = '\0';
64
- req->query[req->query_count].key = pair;
65
- req->query[req->query_count].value = eq + 1;
66
- url_decode((char *)req->query[req->query_count].key);
67
- url_decode((char *)req->query[req->query_count].value);
68
- } else {
69
- req->query[req->query_count].key = pair;
70
- req->query[req->query_count].value = "";
71
- }
72
- req->query_count++;
73
- pair = strtok_r(NULL, "&", &saveptr);
55
+ static void parse_query_string(char* qs, cerver_request_t* req) {
56
+ if (!qs || !*qs) return;
57
+
58
+ /* Parse directly on the buffer — no strdup needed */
59
+ char* p = qs;
60
+
61
+ while (*p && req->query_count < CERVER_MAX_QUERY) {
62
+ char* pair_start = p;
63
+
64
+ /* Find end of pair (& or NUL) */
65
+ while (*p && *p != '&') p++;
66
+ if (*p == '&') *p++ = '\0';
67
+
68
+ char* eq = strchr(pair_start, '=');
69
+ if (eq) {
70
+ *eq = '\0';
71
+ req->query[req->query_count].key = pair_start;
72
+ req->query[req->query_count].value = eq + 1;
73
+ url_decode((char*)req->query[req->query_count].key);
74
+ url_decode((char*)req->query[req->query_count].value);
75
+ } else {
76
+ req->query[req->query_count].key = pair_start;
77
+ req->query[req->query_count].value = "";
74
78
  }
79
+ req->query_count++;
80
+ }
75
81
  }
76
82
 
77
83
  /* ------------------------------------------------------------------ */
78
- /* Parse the HTTP request */
84
+ /* Parse the HTTP request IN-PLACE */
79
85
  /* ------------------------------------------------------------------ */
80
86
 
81
- int cerver_parse_request(const char *raw, size_t len, cerver_request_t *req) {
82
- if (!raw || len == 0) return -1;
83
-
84
- /* We need a mutable copy because we'll be inserting NUL terminators */
85
- char *buf = malloc(len + 1);
86
- if (!buf) return -1;
87
- memcpy(buf, raw, len);
88
- buf[len] = '\0';
89
-
90
- req->_raw_buf = buf;
91
- req->_raw_len = len;
92
-
93
- /* ---- Request line: METHOD PATH HTTP/1.x ---- */
94
- char *line_end = strstr(buf, "\r\n");
95
- if (!line_end) {
96
- free(buf);
97
- return -1;
98
- }
99
- *line_end = '\0';
100
-
101
- /* Method */
102
- char *sp1 = strchr(buf, ' ');
103
- if (!sp1) { free(buf); return -1; }
104
- *sp1 = '\0';
105
-
106
- size_t method_len = (size_t)(sp1 - buf);
107
- if (method_len >= sizeof(req->method)) method_len = sizeof(req->method) - 1;
108
- memcpy(req->method, buf, method_len);
109
- req->method[method_len] = '\0';
110
-
111
- /* Path (and maybe query string) */
112
- char *path_start = sp1 + 1;
113
- char *sp2 = strchr(path_start, ' ');
114
- if (sp2) *sp2 = '\0';
115
-
116
- /* Split path and query string */
117
- char *qmark = strchr(path_start, '?');
118
- if (qmark) {
119
- *qmark = '\0';
120
- strncpy(req->query_string, qmark + 1, sizeof(req->query_string) - 1);
121
- req->query_string[sizeof(req->query_string) - 1] = '\0';
122
- }
123
-
124
- /* Decode and store path */
125
- url_decode(path_start);
126
- strncpy(req->path, path_start, sizeof(req->path) - 1);
127
- req->path[sizeof(req->path) - 1] = '\0';
128
-
129
- /* Normalize trailing slash: "/foo/" "/foo" (but keep "/" as is) */
130
- size_t plen = strlen(req->path);
131
- if (plen > 1 && req->path[plen - 1] == '/') {
132
- req->path[plen - 1] = '\0';
87
+ int cerver_parse_request(const char* raw, size_t len, cerver_request_t* req) {
88
+ if (!raw || len == 0) return -1;
89
+
90
+ /*
91
+ * We parse in-place: the caller gives us a mutable buffer (cast away
92
+ * const the caller's read_full_request already owns a mutable buffer).
93
+ * All internal pointers (headers, query, body) reference this buffer.
94
+ * The caller must keep it alive for the request's lifetime.
95
+ */
96
+ char* buf = (char*)raw;
97
+ buf[len] = '\0'; /* caller ensures buf has capacity for len+1 */
98
+
99
+ /* We no longer allocate _raw_buf the read buffer IS the raw buffer */
100
+ req->_raw_buf = NULL;
101
+ req->_raw_len = len;
102
+
103
+ /* ---- Request line: METHOD PATH HTTP/1.x ---- */
104
+ char* line_end = strstr(buf, "\r\n");
105
+ if (!line_end) return -1;
106
+ *line_end = '\0';
107
+
108
+ /* Method */
109
+ char* sp1 = strchr(buf, ' ');
110
+ if (!sp1) return -1;
111
+ *sp1 = '\0';
112
+
113
+ size_t method_len = (size_t)(sp1 - buf);
114
+ if (method_len >= sizeof(req->method)) method_len = sizeof(req->method) - 1;
115
+ memcpy(req->method, buf, method_len);
116
+ req->method[method_len] = '\0';
117
+
118
+ /* Path (and maybe query string) */
119
+ char* path_start = sp1 + 1;
120
+ char* sp2 = strchr(path_start, ' ');
121
+ if (sp2) *sp2 = '\0';
122
+
123
+ /* Split path and query string */
124
+ char* qmark = strchr(path_start, '?');
125
+ if (qmark) {
126
+ *qmark = '\0';
127
+ /* Point query_string directly into the buffer */
128
+ char* qs_start = qmark + 1;
129
+ size_t qs_len = strlen(qs_start);
130
+ if (qs_len >= sizeof(req->query_string)) qs_len = sizeof(req->query_string) - 1;
131
+ memcpy(req->query_string, qs_start, qs_len);
132
+ req->query_string[qs_len] = '\0';
133
+
134
+ /* Parse query params in-place from query_string
135
+ * (we copied to req->query_string so params point into req memory) */
136
+ parse_query_string(req->query_string, req);
137
+ }
138
+
139
+ /* Decode and store path */
140
+ url_decode(path_start);
141
+ strncpy(req->path, path_start, sizeof(req->path) - 1);
142
+ req->path[sizeof(req->path) - 1] = '\0';
143
+
144
+ /* Normalize trailing slash: "/foo/" → "/foo" (but keep "/" as is) */
145
+ size_t plen = strlen(req->path);
146
+ if (plen > 1 && req->path[plen - 1] == '/') {
147
+ req->path[plen - 1] = '\0';
148
+ }
149
+
150
+ /* ---- Headers ---- */
151
+ char* hdr_start = line_end + 2; /* skip \r\n */
152
+ size_t content_length = 0;
153
+
154
+ while (hdr_start < buf + len) {
155
+ char* hdr_end = strstr(hdr_start, "\r\n");
156
+ if (!hdr_end) break;
157
+
158
+ /* Empty line = end of headers */
159
+ if (hdr_end == hdr_start) {
160
+ hdr_start = hdr_end + 2;
161
+ break;
133
162
  }
134
163
 
135
- /* Parse query string */
136
- if (req->query_string[0]) {
137
- /* We need a mutable copy for strtok */
138
- char *qs_copy = strdup(req->query_string);
139
- if (qs_copy) {
140
- parse_query_string(qs_copy, req);
141
- /* Note: keys/values point into qs_copy which we leak intentionally
142
- since the request's lifetime is short (one connection). */
143
- }
144
- }
145
-
146
- /* ---- Headers ---- */
147
- char *hdr_start = line_end + 2; /* skip \r\n */
148
- size_t content_length = 0;
149
-
150
- while (hdr_start < buf + len) {
151
- char *hdr_end = strstr(hdr_start, "\r\n");
152
- if (!hdr_end) break;
164
+ *hdr_end = '\0';
153
165
 
154
- /* Empty line = end of headers */
155
- if (hdr_end == hdr_start) {
156
- hdr_start = hdr_end + 2;
157
- break;
158
- }
159
-
160
- *hdr_end = '\0';
161
-
162
- if (req->header_count < CERVER_MAX_HEADERS) {
163
- char *colon = strchr(hdr_start, ':');
164
- if (colon) {
165
- *colon = '\0';
166
- char *val = colon + 1;
167
- while (*val == ' ') val++;
166
+ if (req->header_count < CERVER_MAX_HEADERS) {
167
+ char* colon = strchr(hdr_start, ':');
168
+ if (colon) {
169
+ *colon = '\0';
170
+ char* val = colon + 1;
171
+ while (*val == ' ') val++;
168
172
 
169
- req->headers[req->header_count].key = hdr_start;
170
- req->headers[req->header_count].value = val;
171
- req->header_count++;
173
+ req->headers[req->header_count].key = hdr_start;
174
+ req->headers[req->header_count].value = val;
175
+ req->header_count++;
172
176
 
173
- /* Track content-length */
174
- if (strcasecmp(hdr_start, "Content-Length") == 0) {
175
- content_length = (size_t)atol(val);
176
- }
177
- }
177
+ /* Track content-length */
178
+ if (strcasecmp(hdr_start, "Content-Length") == 0) {
179
+ content_length = (size_t)atol(val);
178
180
  }
179
-
180
- hdr_start = hdr_end + 2;
181
+ }
181
182
  }
182
183
 
183
- /* ---- Body (for POST etc.) ---- */
184
- if (content_length > 0 && hdr_start < buf + len) {
185
- req->body = hdr_start;
186
- req->body_len = content_length;
187
- /* Ensure we don't read past the buffer */
188
- size_t remaining = (size_t)(buf + len - hdr_start);
189
- if (req->body_len > remaining) {
190
- req->body_len = remaining;
191
- }
184
+ hdr_start = hdr_end + 2;
185
+ }
186
+
187
+ /* ---- Body (for POST etc.) ---- */
188
+ if (content_length > 0 && hdr_start < buf + len) {
189
+ req->body = hdr_start;
190
+ req->body_len = content_length;
191
+ /* Ensure we don't read past the buffer */
192
+ size_t remaining = (size_t)(buf + len - hdr_start);
193
+ if (req->body_len > remaining) {
194
+ req->body_len = remaining;
192
195
  }
196
+ }
193
197
 
194
- return 0;
198
+ return 0;
195
199
  }
@@ -1,7 +1,8 @@
1
1
  /*
2
2
  * http_writer.c — HTTP/1.1 response writer.
3
3
  *
4
- * Formats a cerver_response_t into raw HTTP bytes and writes to a socket fd.
4
+ * Uses writev() for zero-copy header+body writes.
5
+ * Supports keep-alive and Connection: close signaling.
5
6
  */
6
7
 
7
8
  #include "cerver.h"
@@ -10,128 +11,168 @@
10
11
  #include <stdlib.h>
11
12
  #include <string.h>
12
13
  #include <unistd.h>
14
+ #include <sys/uio.h>
13
15
 
14
16
  /* ------------------------------------------------------------------ */
15
17
  /* Status text lookup */
16
18
  /* ------------------------------------------------------------------ */
17
19
 
18
- static const char *status_text(int code) {
19
- switch (code) {
20
- case 200: return "OK";
21
- case 201: return "Created";
22
- case 204: return "No Content";
23
- case 301: return "Moved Permanently";
24
- case 302: return "Found";
25
- case 304: return "Not Modified";
26
- case 400: return "Bad Request";
27
- case 401: return "Unauthorized";
28
- case 403: return "Forbidden";
29
- case 404: return "Not Found";
30
- case 405: return "Method Not Allowed";
31
- case 500: return "Internal Server Error";
32
- default: return "Unknown";
33
- }
20
+ static const char* status_text(int code) {
21
+ switch (code) {
22
+ case 200:
23
+ return "OK";
24
+ case 201:
25
+ return "Created";
26
+ case 204:
27
+ return "No Content";
28
+ case 301:
29
+ return "Moved Permanently";
30
+ case 302:
31
+ return "Found";
32
+ case 304:
33
+ return "Not Modified";
34
+ case 400:
35
+ return "Bad Request";
36
+ case 401:
37
+ return "Unauthorized";
38
+ case 403:
39
+ return "Forbidden";
40
+ case 404:
41
+ return "Not Found";
42
+ case 405:
43
+ return "Method Not Allowed";
44
+ case 500:
45
+ return "Internal Server Error";
46
+ case 503:
47
+ return "Service Unavailable";
48
+ default:
49
+ return "Unknown";
50
+ }
34
51
  }
35
52
 
36
53
  /* ------------------------------------------------------------------ */
37
- /* Write the full response to fd */
54
+ /* Write the full response to fd using writev */
38
55
  /* ------------------------------------------------------------------ */
39
56
 
40
- int cerver_write_response(int fd, const cerver_response_t *res) {
41
- /* Build the response header */
42
- char header[4096];
43
- int hlen = 0;
44
-
45
- /* Status line */
46
- hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen,
47
- "HTTP/1.1 %d %s\r\n", res->status, status_text(res->status));
48
-
49
- /* Content-Type */
50
- if (res->content_type) {
51
- hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen,
52
- "Content-Type: %s\r\n", res->content_type);
57
+ int cerver_write_response(int fd, const cerver_response_t* res, int keepalive) {
58
+ /* Build the response header */
59
+ char header[4096];
60
+ int hlen = 0;
61
+
62
+ /* Status line */
63
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "HTTP/1.1 %d %s\r\n", res->status,
64
+ status_text(res->status));
65
+
66
+ /* Content-Type */
67
+ if (res->content_type) {
68
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "Content-Type: %s\r\n",
69
+ res->content_type);
70
+ }
71
+
72
+ /* Content-Length */
73
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "Content-Length: %zu\r\n",
74
+ res->body_len);
75
+
76
+ /* Extra headers */
77
+ for (int i = 0; i < res->header_count; i++) {
78
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "%s: %s\r\n",
79
+ res->headers[i].key, res->headers[i].value);
80
+ }
81
+
82
+ /* Connection header — honor keep-alive state */
83
+ if (keepalive && !res->_force_close) {
84
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "Connection: keep-alive\r\n");
85
+ } else {
86
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "Connection: close\r\n");
87
+ }
88
+
89
+ /* Server header */
90
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "Server: cerver\r\n");
91
+
92
+ /* End of headers */
93
+ hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "\r\n");
94
+
95
+ /*
96
+ * Use writev() to send header + body in a single syscall.
97
+ * This avoids Nagle interaction and reduces context switches.
98
+ */
99
+ if (res->body && res->body_len > 0) {
100
+ struct iovec iov[2];
101
+ iov[0].iov_base = header;
102
+ iov[0].iov_len = (size_t)hlen;
103
+ iov[1].iov_base = (void*)res->body;
104
+ iov[1].iov_len = res->body_len;
105
+
106
+ size_t total = iov[0].iov_len + iov[1].iov_len;
107
+ size_t written = 0;
108
+
109
+ while (written < total) {
110
+ ssize_t n = writev(fd, iov, 2);
111
+ if (n < 0) return -1;
112
+ written += (size_t)n;
113
+
114
+ /* Adjust iov for partial writes */
115
+ if (written < iov[0].iov_len) {
116
+ iov[0].iov_base = header + written;
117
+ iov[0].iov_len -= (size_t)n;
118
+ } else {
119
+ /* Header fully sent, adjust body iov */
120
+ size_t body_sent = written - (size_t)hlen;
121
+ iov[0].iov_len = 0;
122
+ iov[1].iov_base = (void*)(res->body + body_sent);
123
+ iov[1].iov_len = res->body_len - body_sent;
124
+ }
53
125
  }
54
-
55
- /* Content-Length */
56
- hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen,
57
- "Content-Length: %zu\r\n", res->body_len);
58
-
59
- /* Extra headers */
60
- for (int i = 0; i < res->header_count; i++) {
61
- hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen,
62
- "%s: %s\r\n",
63
- res->headers[i].key, res->headers[i].value);
64
- }
65
-
66
- /* Connection: close (we don't do keep-alive in v0.1) */
67
- hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen,
68
- "Connection: close\r\n");
69
-
70
- /* Server header */
71
- hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen,
72
- "Server: cerver\r\n");
73
-
74
- /* End of headers */
75
- hlen += snprintf(header + hlen, sizeof(header) - (size_t)hlen, "\r\n");
76
-
77
- /* Write header */
126
+ } else {
127
+ /* No body — just send header */
78
128
  ssize_t written = write(fd, header, (size_t)hlen);
79
129
  if (written < 0) return -1;
130
+ }
80
131
 
81
- /* Write body */
82
- if (res->body && res->body_len > 0) {
83
- size_t total = 0;
84
- while (total < res->body_len) {
85
- ssize_t n = write(fd, res->body + total, res->body_len - total);
86
- if (n < 0) return -1;
87
- total += (size_t)n;
88
- }
89
- }
90
-
91
- return 0;
132
+ return 0;
92
133
  }
93
134
 
94
135
  /* ------------------------------------------------------------------ */
95
136
  /* Response helper functions */
96
137
  /* ------------------------------------------------------------------ */
97
138
 
98
- void cerver_res_text(cerver_response_t *res, int status, const char *text) {
99
- res->status = status;
100
- res->content_type = "text/plain; charset=utf-8";
101
- res->body = text;
102
- res->body_len = strlen(text);
103
- res->_body_owned = 0;
139
+ void cerver_res_text(cerver_response_t* res, int status, const char* text) {
140
+ res->status = status;
141
+ res->content_type = "text/plain; charset=utf-8";
142
+ res->body = text;
143
+ res->body_len = strlen(text);
144
+ res->_body_owned = 0;
104
145
  }
105
146
 
106
- void cerver_res_json(cerver_response_t *res, int status, const char *json) {
107
- res->status = status;
108
- res->content_type = "application/json; charset=utf-8";
109
- res->body = json;
110
- res->body_len = strlen(json);
111
- res->_body_owned = 0;
147
+ void cerver_res_json(cerver_response_t* res, int status, const char* json) {
148
+ res->status = status;
149
+ res->content_type = "application/json; charset=utf-8";
150
+ res->body = json;
151
+ res->body_len = strlen(json);
152
+ res->_body_owned = 0;
112
153
  }
113
154
 
114
- void cerver_res_html(cerver_response_t *res, int status, const char *html) {
115
- res->status = status;
116
- res->content_type = "text/html; charset=utf-8";
117
- res->body = html;
118
- res->body_len = strlen(html);
119
- res->_body_owned = 0;
155
+ void cerver_res_html(cerver_response_t* res, int status, const char* html) {
156
+ res->status = status;
157
+ res->content_type = "text/html; charset=utf-8";
158
+ res->body = html;
159
+ res->body_len = strlen(html);
160
+ res->_body_owned = 0;
120
161
  }
121
162
 
122
- void cerver_res_file(cerver_response_t *res, int status, const char *mime,
123
- const unsigned char *data, size_t len) {
124
- res->status = status;
125
- res->content_type = mime;
126
- res->body = (const char *)data;
127
- res->body_len = len;
128
- res->_body_owned = 0;
163
+ void cerver_res_file(cerver_response_t* res, int status, const char* mime,
164
+ const unsigned char* data, size_t len) {
165
+ res->status = status;
166
+ res->content_type = mime;
167
+ res->body = (const char*)data;
168
+ res->body_len = len;
169
+ res->_body_owned = 0;
129
170
  }
130
171
 
131
- void cerver_res_header(cerver_response_t *res, const char *key, const char *val) {
132
- if (res->header_count < CERVER_MAX_HEADERS) {
133
- res->headers[res->header_count].key = key;
134
- res->headers[res->header_count].value = val;
135
- res->header_count++;
136
- }
172
+ void cerver_res_header(cerver_response_t* res, const char* key, const char* val) {
173
+ if (res->header_count < CERVER_MAX_HEADERS) {
174
+ res->headers[res->header_count].key = key;
175
+ res->headers[res->header_count].value = val;
176
+ res->header_count++;
177
+ }
137
178
  }