Skip to content

Commit

Permalink
Reorganize echttp_static.c to make it an optional extension of echttp…
Browse files Browse the repository at this point in the history
….c (remove dependencies in echttp.c)
  • Loading branch information
pascal-fb-martin committed Oct 6, 2019
1 parent d8a2ca3 commit 8c189a0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 40 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ install:
chown root:root /usr/local/lib/libechttp.a
chmod 644 /usr/local/lib/libechttp.a
mkdir -p /usr/local/include
cp echttp.h /usr/local/include
cp echttp.h echttp_static.h /usr/local/include
chown root:root /usr/local/include/echttp.h
chmod 644 /usr/local/include/echttp.h
chown root:root /usr/local/include/echttp_static.h
chmod 644 /usr/local/include/echttp.h /usr/local/include/echttp_static.h

clean:
rm -f *.o *.a
Expand Down
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ cc -o httpserver httpserver.c -lechttp
```

## API
### Base HTTP Server
The application must include echttp.h as a prerequisit to using the echttp API.

The echttp primitives are:
```
int echttp_open (int argc, const char **argv);
```
Expand All @@ -43,10 +47,6 @@ int echttp_route_match (const char *root, echttp_callback *call);
```
Defines a route for a parent URI and all its children.
```
int echttp_route_static (const char *uri, const char *path);
```
Associate a parent URI with a local directory path: a child of the specified URI must match an existing file at the specified path.
```
const char *echttp_attribute_get (const char *name);
```
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.
Expand Down Expand Up @@ -89,3 +89,16 @@ Return true if the HTTP debug option was selected. Only used for debug or troubl
void echttp_close (void);
```
Immediately close the HTTP server and all current HTTP connections.
### Static Pages Extension
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.
The static page extension primitives are:
```
void echttp_static_content_map (const char *extension, const char *content);
```
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.
```
int echttp_static_route (const char *uri, const char *path);
```
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).

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.
15 changes: 1 addition & 14 deletions echttp.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
* Defines a route for a parent URI and all its children.
*
*
* int echttp_route_static (const char *uri, const char *path);
*
* Associate a parent URI with a local directory path: a child of
* the specified URI must match an existing file at the specified path.
*
*
* const char *echttp_attribute_get (const char *name);
*
* Get the value of the specified HTTP attribute, or 0 if not found.
Expand Down Expand Up @@ -102,9 +96,8 @@
#include <ctype.h>

#include "echttp.h"
#include "echttp_catalog.h"
#include "echttp_raw.h"
#include "echttp_static.h"
#include "echttp_catalog.h"

static int echttp_debug = 0;

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

int echttp_route_static (const char *uri, const char *path) {
echttp_static_map (uri, path);
return echttp_route_add (uri, echttp_static_page, ECHTTP_MODE_PARENT);
}


const char *echttp_attribute_get (const char *name) {
return echttp_catalog_get (&(echttp_current->in), name);
}
Expand Down
1 change: 0 additions & 1 deletion echttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ typedef const char *echttp_callback (const char *method, const char *uri,

int echttp_route_uri (const char *uri, echttp_callback *call);
int echttp_route_match (const char *root, echttp_callback *call);
int echttp_route_static (const char *uri, const char *path);

const char *echttp_attribute_get (const char *name);
const char *echttp_parameter_get (const char *name);
Expand Down
28 changes: 15 additions & 13 deletions echttp_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <stdio.h>

#include "echttp.h"
#include "echttp_static.h"
#include "echttp_catalog.h"

static echttp_catalog echttp_static_roots;
Expand All @@ -61,19 +62,9 @@ static void echttp_static_initialize (void) {
}
}

void echttp_static_content_map (const char *extension, const char *content) {
echttp_static_initialize();
echttp_catalog_set (&echttp_static_type, extension, content);
}

void echttp_static_map (const char *uri, const char *path) {
echttp_static_initialize();
echttp_catalog_set (&echttp_static_roots, uri, path);
}

const char *echttp_static_page (const char *action,
const char *uri,
const char *data, int length) {
static const char *echttp_static_page (const char *action,
const char *uri,
const char *data, int length) {
const char *path;
char rooturi[1024];
char filename[1024];
Expand Down Expand Up @@ -135,3 +126,14 @@ const char *echttp_static_page (const char *action,
return echttp_static_buffer;
}

void echttp_static_content_map (const char *extension, const char *content) {
echttp_static_initialize();
echttp_catalog_set (&echttp_static_type, extension, content);
}

int echttp_static_route (const char *uri, const char *path) {
echttp_static_initialize();
echttp_catalog_set (&echttp_static_roots, uri, path);
echttp_route_match (uri, echttp_static_page);
}

8 changes: 3 additions & 5 deletions echttp_static.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
*
* A minimal HTTP server library designed for simplicity and embedding in
* existing applications.
*
* echttp_static.h - An additional module to serve static page (files).
*/
void echttp_static_content_map (const char *extension, const char *content);

void echttp_static_map (const char *uri, const char *path);

const char *echttp_static_page (const char *action,
const char *uri,
const char *data, int length);
int echttp_static_route (const char *uri, const char *path);

3 changes: 2 additions & 1 deletion test/httpserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <unistd.h>

#include "echttp.h"
#include "echttp_static.h"

const char *http_welcome (const char *method, const char *uri,
const char *data, int length) {
Expand Down Expand Up @@ -81,7 +82,7 @@ int main (int argc, const char **argv) {
echttp_route_uri ("/welcome", http_welcome);
echttp_route_uri ("/whoami", http_whoami);
echttp_route_match ("/echo", http_echo);
echttp_route_static ("/static", getcwd(0, 0));
echttp_static_route ("/static", getcwd(0, 0));

echttp_listen (0, 1, http_console, 1);

Expand Down

0 comments on commit 8c189a0

Please sign in to comment.