Skip to content

Commit

Permalink
Merge pull request #43 from widgetii/http_server
Browse files Browse the repository at this point in the history
Http server
  • Loading branch information
azat authored Feb 18, 2024
2 parents f8fa5f1 + 079bc00 commit ff17091
Show file tree
Hide file tree
Showing 9 changed files with 529 additions and 2 deletions.
63 changes: 63 additions & 0 deletions .clang-format
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
...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/examples_R8/R8_echo_server
/examples_R9/R9_multilookup
/examples_R9/R9_dns_server
/examples_R10/R10_simple_server
/examples_R10/R10_static_server

/tmpcode*

Expand Down
3 changes: 1 addition & 2 deletions LibeventBook.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ include::Ref8_listener.txt[]

include::Ref9_dns.txt[]



include::Ref10_http_server.txt[]

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ GENERATED_CHAPTERS= \
Ref7_evbuffer.html \
Ref8_listener.html \
Ref9_dns.html \
Ref10_http_server.html \
license_bsd.html

GENERATED_HTML = $(GENERATED_METAFILES) $(GENERATED_CHAPTERS)
Expand All @@ -42,6 +43,7 @@ examples:
cd examples_R6a && $(MAKE)
cd examples_R8 && $(MAKE)
cd examples_R9 && $(MAKE)
cd examples_R10 && $(MAKE)

inline_examples:
./bin/build_examples.py *_*.txt
Expand All @@ -66,6 +68,7 @@ Ref6a_advanced_bufferevent.html: examples_R6a/*.c license.txt
Ref7_evbuffer.html: license.txt
Ref8_listener.html: examples_R8/*.c license.txt
Ref9_dns.html: examples_R9/*.c license.txt
Ref10_http_server.html: examples_R10/*.c license.txt

clean:
rm -f *~
Expand Down
57 changes: 57 additions & 0 deletions Ref10_http_server.txt
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`
1 change: 1 addition & 0 deletions TOC.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ A Libevent Reference Manual
- link:Ref7_evbuffer.html[R7: Evbuffers: utility functionality for buffered IO]
- link:Ref8_listener.html[R8: Connection listeners: accepting TCP connections]
- link:Ref9_dns.html[R9: DNS for Libevent]
- link:Ref10_http_server.html[R10: HTTP server]

include::license.txt[]

23 changes: 23 additions & 0 deletions examples_R10/Makefile
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)
49 changes: 49 additions & 0 deletions examples_R10/R10_simple_server.c
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);
}
Loading

0 comments on commit ff17091

Please sign in to comment.