Skip to content

Commit 8c189a0

Browse files
Reorganize echttp_static.c to make it an optional extension of echttp.c (remove dependencies in echttp.c)
1 parent d8a2ca3 commit 8c189a0

File tree

7 files changed

+41
-40
lines changed

7 files changed

+41
-40
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ install:
99
chown root:root /usr/local/lib/libechttp.a
1010
chmod 644 /usr/local/lib/libechttp.a
1111
mkdir -p /usr/local/include
12-
cp echttp.h /usr/local/include
12+
cp echttp.h echttp_static.h /usr/local/include
1313
chown root:root /usr/local/include/echttp.h
14-
chmod 644 /usr/local/include/echttp.h
14+
chown root:root /usr/local/include/echttp_static.h
15+
chmod 644 /usr/local/include/echttp.h /usr/local/include/echttp_static.h
1516

1617
clean:
1718
rm -f *.o *.a

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ cc -o httpserver httpserver.c -lechttp
2020
```
2121

2222
## API
23+
### Base HTTP Server
24+
The application must include echttp.h as a prerequisit to using the echttp API.
25+
26+
The echttp primitives are:
2327
```
2428
int echttp_open (int argc, const char **argv);
2529
```
@@ -43,10 +47,6 @@ int echttp_route_match (const char *root, echttp_callback *call);
4347
```
4448
Defines a route for a parent URI and all its children.
4549
```
46-
int echttp_route_static (const char *uri, const char *path);
47-
```
48-
Associate a parent URI with a local directory path: a child of the specified URI must match an existing file at the specified path.
49-
```
5050
const char *echttp_attribute_get (const char *name);
5151
```
5252
Retrieve the value of the specified HTTP attribute, or 0 if not found. This function should be called from within an HTTP callback, while processing an HTTP request.
@@ -89,3 +89,16 @@ Return true if the HTTP debug option was selected. Only used for debug or troubl
8989
void echttp_close (void);
9090
```
9191
Immediately close the HTTP server and all current HTTP connections.
92+
### Static Pages Extension
93+
The echttp library can serve local files, from several separate locations if needed. This capacity is a separate extension and requires to include echttp_static.h.
94+
The static page extension primitives are:
95+
```
96+
void echttp_static_content_map (const char *extension, const char *content);
97+
```
98+
Define the content type associated with a specific file extension. The content type is implicitely defined for the following file extensions: html, htm, json, jsn, css.
99+
```
100+
int echttp_static_route (const char *uri, const char *path);
101+
```
102+
Associate a parent URI with a local directory path: a child of the specified URI will match an existing file at the specified path (including the URI's child relative path).
103+
104+
For example if one defines a static route from /static to /home/doe/public, the URI /static/fancy/interface.html will route to /home/doe/public/fancy/interface.html.

echttp.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@
4040
* Defines a route for a parent URI and all its children.
4141
*
4242
*
43-
* int echttp_route_static (const char *uri, const char *path);
44-
*
45-
* Associate a parent URI with a local directory path: a child of
46-
* the specified URI must match an existing file at the specified path.
47-
*
48-
*
4943
* const char *echttp_attribute_get (const char *name);
5044
*
5145
* Get the value of the specified HTTP attribute, or 0 if not found.
@@ -102,9 +96,8 @@
10296
#include <ctype.h>
10397

10498
#include "echttp.h"
105-
#include "echttp_catalog.h"
10699
#include "echttp_raw.h"
107-
#include "echttp_static.h"
100+
#include "echttp_catalog.h"
108101

109102
static int echttp_debug = 0;
110103

@@ -478,12 +471,6 @@ int echttp_route_match (const char *root, echttp_callback *call) {
478471
return echttp_route_add (root, call, ECHTTP_MODE_PARENT);
479472
}
480473

481-
int echttp_route_static (const char *uri, const char *path) {
482-
echttp_static_map (uri, path);
483-
return echttp_route_add (uri, echttp_static_page, ECHTTP_MODE_PARENT);
484-
}
485-
486-
487474
const char *echttp_attribute_get (const char *name) {
488475
return echttp_catalog_get (&(echttp_current->in), name);
489476
}

echttp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ typedef const char *echttp_callback (const char *method, const char *uri,
2323

2424
int echttp_route_uri (const char *uri, echttp_callback *call);
2525
int echttp_route_match (const char *root, echttp_callback *call);
26-
int echttp_route_static (const char *uri, const char *path);
2726

2827
const char *echttp_attribute_get (const char *name);
2928
const char *echttp_parameter_get (const char *name);

echttp_static.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <stdio.h>
3939

4040
#include "echttp.h"
41+
#include "echttp_static.h"
4142
#include "echttp_catalog.h"
4243

4344
static echttp_catalog echttp_static_roots;
@@ -61,19 +62,9 @@ static void echttp_static_initialize (void) {
6162
}
6263
}
6364

64-
void echttp_static_content_map (const char *extension, const char *content) {
65-
echttp_static_initialize();
66-
echttp_catalog_set (&echttp_static_type, extension, content);
67-
}
68-
69-
void echttp_static_map (const char *uri, const char *path) {
70-
echttp_static_initialize();
71-
echttp_catalog_set (&echttp_static_roots, uri, path);
72-
}
73-
74-
const char *echttp_static_page (const char *action,
75-
const char *uri,
76-
const char *data, int length) {
65+
static const char *echttp_static_page (const char *action,
66+
const char *uri,
67+
const char *data, int length) {
7768
const char *path;
7869
char rooturi[1024];
7970
char filename[1024];
@@ -135,3 +126,14 @@ const char *echttp_static_page (const char *action,
135126
return echttp_static_buffer;
136127
}
137128

129+
void echttp_static_content_map (const char *extension, const char *content) {
130+
echttp_static_initialize();
131+
echttp_catalog_set (&echttp_static_type, extension, content);
132+
}
133+
134+
int echttp_static_route (const char *uri, const char *path) {
135+
echttp_static_initialize();
136+
echttp_catalog_set (&echttp_static_roots, uri, path);
137+
echttp_route_match (uri, echttp_static_page);
138+
}
139+

echttp_static.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
*
33
* A minimal HTTP server library designed for simplicity and embedding in
44
* existing applications.
5+
*
6+
* echttp_static.h - An additional module to serve static page (files).
57
*/
68
void echttp_static_content_map (const char *extension, const char *content);
79

8-
void echttp_static_map (const char *uri, const char *path);
9-
10-
const char *echttp_static_page (const char *action,
11-
const char *uri,
12-
const char *data, int length);
10+
int echttp_static_route (const char *uri, const char *path);
1311

test/httpserver.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <unistd.h>
3131

3232
#include "echttp.h"
33+
#include "echttp_static.h"
3334

3435
const char *http_welcome (const char *method, const char *uri,
3536
const char *data, int length) {
@@ -81,7 +82,7 @@ int main (int argc, const char **argv) {
8182
echttp_route_uri ("/welcome", http_welcome);
8283
echttp_route_uri ("/whoami", http_whoami);
8384
echttp_route_match ("/echo", http_echo);
84-
echttp_route_static ("/static", getcwd(0, 0));
85+
echttp_static_route ("/static", getcwd(0, 0));
8586

8687
echttp_listen (0, 1, http_console, 1);
8788

0 commit comments

Comments
 (0)