Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
added some new functions to examples/eutils
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFrench committed Dec 11, 2017
1 parent 8f50b83 commit d890899
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 45 deletions.
15 changes: 15 additions & 0 deletions examples/eutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ static void * mm__dup_(const void * src, size_t size) {

#define mm__alloc_(type, ...) \
(type *)mm__dup_((type[]) {__VA_ARGS__ }, sizeof(type))

#define bind__sock_port0_(HTP) ({ \
struct sockaddr_in sin; \
socklen_t len = len = sizeof(struct sockaddr); \
uint16_t port; \
\
evhtp_bind_socket(HTP, "127.0.0.1", 0, 128); \
\
getsockname( \
evconnlistener_get_fd(HTP->server), \
(struct sockaddr *)&sin, &len); \
\
port = ntohs(sin.sin_port); \
port; \
})
15 changes: 1 addition & 14 deletions examples/example_chunked.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,7 @@ main(int argc, char ** argv) {
*/
evhtp_set_gencb(htp, http__callback_, strdup(argv[1]));

evhtp_bind_socket(htp, "127.0.0.1", 0, 128);
{
struct sockaddr_in sin;
socklen_t len = sizeof(struct sockaddr);
uint16_t port;

getsockname(
evconnlistener_get_fd(htp->server),
(struct sockaddr *)&sin, &len);

port = ntohs(sin.sin_port);

log_info("curl http://127.0.0.1:%d/", port);
}
log_info("curl http://127.0.0.1:%d/", bind__sock_port0_(htp));

event_base_loop(evbase, 0);

Expand Down
22 changes: 4 additions & 18 deletions examples/example_pause.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../log.h"
#include "internal.h"
#include "evhtp/evhtp.h"
#include "./eutils.h"

struct paused_request_ {
struct event * _timeoutev;
Expand Down Expand Up @@ -103,23 +104,8 @@ main(int argc, char ** argv) {
*/
evhtp_set_gencb(htp, http_pause__callback_, &timeo);

evhtp_bind_socket(htp, "127.0.0.1", 0, 128);
{
struct sockaddr_in sin;
socklen_t len = sizeof(struct sockaddr);
uint16_t port;
log_info("response delayed for 10s: "
"curl http://127.0.0.1:%d/", bind__sock_port0_(htp));

getsockname(
evconnlistener_get_fd(htp->server),
(struct sockaddr *)&sin, &len);

port = ntohs(sin.sin_port);

log_info("request will be delayed for 10 seconds with: curl http://127.0.0.1:%d/", port);
}

event_base_loop(evbase, 0);


return 0;
return event_base_loop(evbase, 0);
}
16 changes: 3 additions & 13 deletions examples/example_vhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "internal.h"
#include "../log.h"
#include "evhtp/evhtp.h"
#include "./eutils.h"

#define make_response(cb) do { \
evbuffer_add_printf(req->buffer_out, \
Expand Down Expand Up @@ -94,19 +95,8 @@ main(int argc, char ** argv) {
/* we can also append a single alias to vhost_2 like this */
evhtp_add_alias(htp_vhost_2, "gmail.google.com");

/* now bind and listen on our server */
evhtp_bind_socket(htp, "127.0.0.1", 0, 128);

{
struct sockaddr_in sin;
socklen_t len = sizeof(struct sockaddr);
uint16_t port;

getsockname(
evconnlistener_get_fd(htp->server),
(struct sockaddr *)&sin, &len);

port = ntohs(sin.sin_port);
uint16_t port = bind__sock_port0_(htp);

log_info("[[ try the following commands and you should see 'evhtp.io domains' ]]");
log_info("=====================================================================");
Expand All @@ -123,5 +113,5 @@ main(int argc, char ** argv) {
log_info("curl -H'Host: gmail.google.com' http://127.0.0.1:%d/vhost", port);
}

event_base_loop(evbase, 0);
return event_base_loop(evbase, 0);
} /* main */

0 comments on commit d890899

Please sign in to comment.