Skip to content

Commit ef81ba1

Browse files
committed
Addressed more reviewers comments
1 parent 3f1cec6 commit ef81ba1

3 files changed

Lines changed: 71 additions & 77 deletions

File tree

src/http/httpd.c

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program; if not, write to the Free Software
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*
21+
*
22+
* This is a simple HTTP server module for wolfIP.
23+
*
24+
* This file contains a basic implementation of a HTTP server
25+
* that can be used with wolfIP.
26+
*
27+
* The HTTP server supports:
28+
* - GET requests
29+
* - POST requests
30+
* - Basic file serving
31+
* - Basic error handling
32+
*
33+
* Usage:
34+
* - Initialize via httpd_init()
35+
* - Add static pages via httpd_register_static_page()
36+
* - Add request handlers via httpd_register_handler()
37+
*
38+
* Note:
39+
* - Responses are sent immediately after generation. No buffering is done.
40+
* If the output socket is flooded, extra responses will be discarded.
41+
*
42+
*
2043
*/
2144
#include "wolfip.h"
2245
#include "httpd.h"
@@ -43,16 +66,6 @@ static const char *http_status_text(int status_code) {
4366
return "Unknown";
4467
}
4568
}
46-
/*
47-
static struct http_client *http_client_find(struct httpd *httpd, int sd) {
48-
for (int i = 0; i < HTTPD_MAX_CLIENTS; i++) {
49-
if (httpd->clients[i].client_sd == sd) {
50-
return &httpd->clients[i];
51-
}
52-
}
53-
return NULL;
54-
}
55-
*/
5669

5770
int httpd_register_handler(struct httpd *httpd, const char *path, int (*handler)(struct httpd *httpd, struct http_client *hc, struct http_request *req)) {
5871
for (int i = 0; i < HTTPD_MAX_URLS; i++) {
@@ -125,75 +138,63 @@ void http_send_response_headers(struct http_client *hc, int status_code, const c
125138
}
126139

127140
void http_send_response_body(struct http_client *hc, const void *body, size_t len) {
128-
if (!hc) return;
129-
if (hc->ssl) {
130-
int rc = wolfSSL_write(hc->ssl, body, len);
131-
if (rc <= 0) {
132-
wolfSSL_free(hc->ssl);
133-
hc->ssl = NULL;
134-
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
135-
hc->client_sd = 0;
136-
}
137-
} else {
138-
int rc = wolfIP_sock_send(hc->httpd->ipstack, hc->client_sd, body, len, 0);
139-
if (rc <= 0) {
140-
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
141-
hc->client_sd = 0;
142-
}
141+
int rc;
142+
if (!hc)
143+
return;
144+
if (hc->ssl)
145+
rc = wolfSSL_write(hc->ssl, body, len);
146+
else
147+
rc = wolfIP_sock_send(hc->httpd->ipstack, hc->client_sd, body, len, 0);
148+
149+
if (rc <= 0) {
150+
wolfSSL_free(hc->ssl);
151+
hc->ssl = NULL;
152+
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
153+
hc->client_sd = 0;
143154
}
144155
}
145156

157+
static int http_write_response(struct http_client *hc, const void *buf, size_t len)
158+
{
159+
struct wolfIP *s;
160+
if (!hc)
161+
return -1;
162+
s = hc->httpd->ipstack;
163+
if (hc->ssl)
164+
return wolfSSL_write(hc->ssl, buf, len);
165+
else
166+
return wolfIP_sock_send(s, hc->client_sd, buf, len, 0);
167+
}
168+
146169
void http_send_response_chunk(struct http_client *hc, const void *chunk, size_t len) {
147170
char txt_chunk[8];
148171
memset(txt_chunk, 0, sizeof(txt_chunk));
149-
if (!hc) return;
172+
if (!hc)
173+
return;
150174
snprintf(txt_chunk, sizeof(txt_chunk), "%zx\r\n", len);
151-
if (hc->ssl) {
152-
int rc = wolfSSL_write(hc->ssl, txt_chunk, strlen(txt_chunk));
153-
if (rc <= 0)
154-
goto close_conn;
155-
rc = wolfSSL_write(hc->ssl, chunk, len);
156-
if (rc <= 0)
157-
goto close_conn;
158-
rc = wolfSSL_write(hc->ssl, "\r\n", 2);
159-
if (rc <= 0)
160-
goto close_conn;
161-
} else {
162-
struct wolfIP *s = hc->httpd->ipstack;
163-
int rc = wolfIP_sock_send(s, hc->client_sd, txt_chunk, strlen(txt_chunk), 0);
164-
if (rc <= 0)
165-
goto close_conn;
166-
rc = wolfIP_sock_send(s, hc->client_sd, chunk, len, 0);
167-
if (rc <= 0)
168-
goto close_conn;
169-
rc = wolfIP_sock_send(s, hc->client_sd, "\r\n", 2, 0);
170-
if (rc <= 0)
171-
goto close_conn;
172-
}
173-
return;
174-
close_conn:
175-
if (hc->ssl) {
175+
if ((http_write_response(hc, txt_chunk, strlen(txt_chunk)) <= 0) ||
176+
(http_write_response(hc, chunk, len) <= 0) ||
177+
(http_write_response(hc, "\r\n", 2) <= 0)) {
176178
wolfSSL_free(hc->ssl);
177179
hc->ssl = NULL;
180+
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
181+
hc->client_sd = 0;
178182
}
179-
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
180-
hc->client_sd = 0;
181183
}
182184

183185
void http_send_response_chunk_end(struct http_client *hc) {
184-
if (!hc) return;
185-
if (hc->ssl) {
186-
if (wolfSSL_write(hc->ssl, "0\r\n\r\n", 5) <= 0) {
187-
wolfSSL_free(hc->ssl);
188-
hc->ssl = NULL;
189-
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
190-
hc->client_sd = 0;
191-
}
192-
} else {
193-
if (wolfIP_sock_send(hc->httpd->ipstack, hc->client_sd, "0\r\n\r\n", 5, 0) <= 0) {
194-
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
195-
hc->client_sd = 0;
196-
}
186+
int rc;
187+
if (!hc)
188+
return;
189+
if (hc->ssl)
190+
rc = wolfSSL_write(hc->ssl, "0\r\n\r\n", 5);
191+
else
192+
rc = wolfIP_sock_send(hc->httpd->ipstack, hc->client_sd, "0\r\n\r\n", 5, 0);
193+
if (rc <= 0) {
194+
wolfSSL_free(hc->ssl);
195+
hc->ssl = NULL;
196+
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
197+
hc->client_sd = 0;
197198
}
198199
}
199200

@@ -369,7 +370,7 @@ static int parse_http_request(struct http_client *hc, uint8_t *buf, size_t len)
369370
url = http_find_url(hc->httpd, req.path);
370371
if (!url)
371372
goto not_found;
372-
373+
373374
if ((url->handler == NULL) && (url->static_content == NULL))
374375
goto service_unavailable;
375376
if (url->handler == NULL) {

src/wolfip.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ static inline int wolfIP_is_loopback_if(unsigned int if_idx)
4949

5050
#if WOLFIP_ENABLE_LOOPBACK
5151
static int wolfIP_loopback_send(struct wolfIP_ll_dev *ll, void *buf, uint32_t len);
52-
static int wolfIP_loopback_poll(struct wolfIP_ll_dev *ll, void *buf, uint32_t len);
5352
#endif
5453
static void wolfIP_recv_on(struct wolfIP *s, unsigned int if_idx, void *buf, uint32_t len);
5554

@@ -588,13 +587,6 @@ struct wolfIP
588587
};
589588

590589
#if WOLFIP_ENABLE_LOOPBACK
591-
static int wolfIP_loopback_poll(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
592-
{
593-
(void)ll;
594-
(void)buf;
595-
(void)len;
596-
return 0;
597-
}
598590

599591
static int wolfIP_loopback_send(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
600592
{
@@ -2374,7 +2366,7 @@ void wolfIP_init(struct wolfIP *s)
23742366
memcpy(loop->mac, loop_mac, sizeof(loop_mac));
23752367
strncpy(loop->ifname, "lo", sizeof(loop->ifname) - 1);
23762368
loop->ifname[sizeof(loop->ifname) - 1] = '\0';
2377-
loop->poll = wolfIP_loopback_poll;
2369+
loop->poll = NULL;
23782370
loop->send = wolfIP_loopback_send;
23792371
}
23802372
if (loop_conf) {

wolfip.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ typedef uint32_t ip4;
2121
#endif
2222

2323
/* Device driver interface */
24-
/* Struct to contain a hw device description */
24+
/* Struct to contain link-layer (ll) device description
25+
*/
2526
struct wolfIP_ll_dev {
2627
uint8_t mac[6];
2728
char ifname[16];

0 commit comments

Comments
 (0)