-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from widgetii/http_server
Http server
- Loading branch information
Showing
9 changed files
with
529 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
Language: Cpp | ||
BasedOnStyle: LLVM | ||
|
||
AccessModifierOffset: -4 | ||
|
||
AlignAfterOpenBracket: DontAlign | ||
AlignEscapedNewlinesLeft: true | ||
# AlignOperands: true | ||
AlignTrailingComments: true | ||
|
||
AllowAllParametersOfDeclarationOnNextLine: true | ||
AllowShortBlocksOnASingleLine: false | ||
AllowShortCaseLabelsOnASingleLine: false | ||
AllowShortFunctionsOnASingleLine: All | ||
AllowShortIfStatementsOnASingleLine: false | ||
AllowShortLoopsOnASingleLine: false | ||
|
||
AlwaysBreakAfterDefinitionReturnType: All | ||
AlwaysBreakBeforeMultilineStrings: false | ||
AlwaysBreakTemplateDeclarations: false | ||
|
||
# BinPackArguments: false | ||
# BinPackParameters: true | ||
|
||
BreakBeforeBinaryOperators: false | ||
BreakBeforeBraces: Custom | ||
BraceWrapping: { AfterFunction: true } | ||
BreakBeforeTernaryOperators: true | ||
BreakConstructorInitializersBeforeComma: true | ||
|
||
ColumnLimit: 80 | ||
|
||
ContinuationIndentWidth: 4 | ||
|
||
DerivePointerAlignment: false #XXX | ||
DisableFormat: false | ||
ExperimentalAutoDetectBinPacking: false #XXX | ||
ForEachMacros: [ LIST_FOREACH, SIMPLEQ_FOREACH, CIRCLEQ_FOREACH, TAILQ_FOREACH, TAILQ_FOREACH_REVERSE, HT_FOREACH ] | ||
|
||
IndentCaseLabels: false | ||
IndentFunctionDeclarationAfterType: false | ||
IndentWidth: 4 | ||
IndentWrappedFunctionNames: false | ||
|
||
KeepEmptyLinesAtTheStartOfBlocks: true | ||
MaxEmptyLinesToKeep: 2 | ||
|
||
PointerAlignment: Right #XXX | ||
|
||
# SpaceAfterCStyleCast: false | ||
SpaceBeforeAssignmentOperators: true | ||
SpaceBeforeParens: ControlStatements | ||
SpaceInEmptyParentheses: false | ||
SpacesBeforeTrailingComments: 1 | ||
SpacesInAngles: false | ||
SpacesInCStyleCastParentheses: false | ||
SpacesInParentheses: false | ||
Standard: Cpp03 | ||
TabWidth: 4 | ||
UseTab: Always | ||
SortIncludes: false | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,5 @@ include::Ref8_listener.txt[] | |
|
||
include::Ref9_dns.txt[] | ||
|
||
|
||
|
||
include::Ref10_http_server.txt[] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
:docinfo: | ||
|
||
Using the built-in HTTP server | ||
============================== | ||
|
||
include::license.txt[] | ||
|
||
:language: C | ||
|
||
The plain network-based Libevent interface is useful if you want to build native | ||
applications, but it is increasingly common to develop an application based | ||
around the HTTP protocol and a web page that loads, or more commonly dynamically | ||
reloads, information. | ||
|
||
To use the Libevent service, you use the same basic structure as already | ||
described for the main network event model, but instead of having to handle the | ||
network interfacing, the HTTP wrapper handles that for you. This turns the | ||
entire process into the four function calls (initialize, start HTTP server, set | ||
HTTP callback function, and enter event loop), plus the contents of the callback | ||
function that will send data back. A very simple example is provided in the listing: | ||
|
||
|
||
.Example: A basic HTTP server | ||
[code,C] | ||
------ | ||
include::examples_R10/R10_simple_server.c[] | ||
------ | ||
|
||
Given the previous example, the basics of the code here should be relatively | ||
self-explanatory. The main elements are the evhttp_set_gencb() function, which | ||
sets the callback function to be used when an HTTP request is received, and the | ||
generic_request_handler() callback function itself, which populates the response | ||
buffer with a simple message to show success. | ||
|
||
The HTTP wrapper provides a wealth of different functionality. For example, | ||
there is a request parser that will extract the query arguments from a typical | ||
request (as you would use in a HTTP request), and you can also set different | ||
handlers to be triggered within different requested paths. | ||
|
||
Let's extend this example act Libevent as Nginx-like server for static content: | ||
|
||
.Example: A static HTTP server implementation | ||
[code,C] | ||
------ | ||
include::examples_R10/R10_static_server.c[] | ||
------ | ||
|
||
As you can see here we've replaced generic_request_handler() by specific | ||
send_file_to_user() handler which processes incoming request: | ||
|
||
* First it checks if HTTP command is equal to `GET` or `HEAD` | ||
|
||
* Then it parses request URI to extract request path and determine file path we | ||
should handle by couple evhttp_request_get_uri()/evhttp_uri_parse() functions | ||
|
||
* After that it decoded URI string from something like `folder%2Fmy%20doc.txt` | ||
to plain `folder/my doc.txt` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
CC=gcc | ||
CFLAGS=-g -Wall $(LEBOOK_CFLAGS) | ||
|
||
EXAMPLE_BINARIES=R10_simple_server R10_static_server | ||
|
||
all: examples | ||
|
||
examples: $(EXAMPLE_BINARIES) | ||
|
||
R10_simple_server: R10_simple_server.o | ||
$(CC) $(CFLAGS) R10_simple_server.o -o R10_simple_server -levent | ||
|
||
R10_static_server: R10_static_server.o | ||
$(CC) $(CFLAGS) R10_static_server.o -o R10_static_server -levent | ||
|
||
.c.o: | ||
$(CC) $(CFLAGS) -c $< | ||
|
||
clean: | ||
rm -f *~ | ||
rm -f *.o | ||
rm -f $(EXAMPLE_BINARIES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <string.h> | ||
#include <signal.h> | ||
#include <event2/buffer.h> | ||
#include <event2/event.h> | ||
#include <event2/http.h> | ||
|
||
static void | ||
generic_request_handler(struct evhttp_request *req, void *ctx) | ||
{ | ||
struct evbuffer *reply = evbuffer_new(); | ||
|
||
evbuffer_add_printf(reply, "It works!"); | ||
evhttp_send_reply(req, HTTP_OK, NULL, reply); | ||
evbuffer_free(reply); | ||
} | ||
|
||
static void | ||
signal_cb(evutil_socket_t fd, short event, void *arg) | ||
{ | ||
printf("%s signal received\n", strsignal(fd)); | ||
event_base_loopbreak(arg); | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
ev_uint16_t http_port = 8080; | ||
char *http_addr = "0.0.0.0"; | ||
struct event_base *base; | ||
struct evhttp *http_server; | ||
struct event *sig_int; | ||
|
||
base = event_base_new(); | ||
|
||
http_server = evhttp_new(base); | ||
evhttp_bind_socket(http_server, http_addr, http_port); | ||
evhttp_set_gencb(http_server, generic_request_handler, NULL); | ||
|
||
sig_int = evsignal_new(base, SIGINT, signal_cb, base); | ||
event_add(sig_int, NULL); | ||
|
||
printf("Listening requests on http://%s:%d\n", http_addr, http_port); | ||
|
||
event_base_dispatch(base); | ||
|
||
evhttp_free(http_server); | ||
event_free(sig_int); | ||
event_base_free(base); | ||
} |
Oops, something went wrong.