客户端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h> #include <event2/event.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h> #define DEFAULT_URL "http://127.0.0.1:8080" void http_request_done(struct evhttp_request *req, void *arg){
char buffer[];
int nread; if (req == NULL) {
printf("request failed\n");
return;
}
fprintf(stderr, "Response line: %d %s\n",
evhttp_request_get_response_code(req),req->response_code_line); while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
buffer, sizeof(buffer)))
> ) {
fwrite(buffer, nread, , stdout);
}
} int main(int argc, char **argv){
char *url = DEFAULT_URL;
if (argc == ) {
url = argv[];
} struct evhttp_uri *http_uri = NULL;
const char *scheme, *host, *path, *query;
char uri[];
int port; http_uri = evhttp_uri_parse(url); scheme = evhttp_uri_get_scheme(http_uri);
if (scheme == NULL || (strcasecmp(scheme, "https") != &&
strcasecmp(scheme, "http") != )) {
printf("url must be http or https\n");
return ;
} host = evhttp_uri_get_host(http_uri);
port = evhttp_uri_get_port(http_uri);
if (port == -) {
port = (strcasecmp(scheme, "http") == ) ? : ;
} path = evhttp_uri_get_path(http_uri);
if (strlen(path) == ) {
path = "/";
} query = evhttp_uri_get_query(http_uri);
if (query == NULL) {
snprintf(uri, sizeof(uri) - , "%s", path);
} else {
snprintf(uri, sizeof(uri) - , "%s?%s", path, query);
}
uri[sizeof(uri) - ] = '\0'; printf("url: %s\n", url);
printf("scheme: %s\n", scheme);
printf("host: %s\n", host);
printf("port: %d\n", port);
printf("path: %s\n", path);
printf("uri: %s\n", uri); struct event_base *base;
struct evhttp_connection *conn;
struct evhttp_request *req; base = event_base_new();
conn = evhttp_connection_base_new(base, NULL, host, port);
req = evhttp_request_new(http_request_done, base); evhttp_add_header(req->output_headers, "Host", host);
//evhttp_add_header(req->output_headers, "Connection", "close"); evhttp_make_request(conn, req, EVHTTP_REQ_GET, uri); event_base_dispatch(base); return ;
}

服务器:

#include <stdio.h>
#include <stdlib.h>
#include <event2/keyvalq_struct.h>
#include <event2/http.h>
#include <event2/event.h>
#include <event2/buffer.h> #define DEFAULT_PORT 8080 void http_request_cb(struct evhttp_request *req, void *arg);
void print_request(struct evhttp_request *req, void *arg);
void php_request_cb(struct evhttp_request *req, void *arg); int main(int argc, char **argv) {
int port = DEFAULT_PORT;
if (argc == ) {
port = atoi(argv[]);
} struct event_base *base = event_base_new(); struct evhttp *http = evhttp_new(base);
evhttp_set_gencb(http, http_request_cb, NULL);
evhttp_set_cb(http, "/index.php", php_request_cb, NULL);
evhttp_bind_socket(http, "0.0.0.0", port); event_base_dispatch(base); return ;
} void php_request_cb(struct evhttp_request *req, void *arg) {
printf("run php_request_cb...\n");
} void http_request_cb(struct evhttp_request *req, void *arg) {
printf("run http_request_cb...\n"); print_request(req, arg); const char *uri = evhttp_request_get_uri(req);
printf("uri: %s\n", uri);
const char *decoded_uri = evhttp_decode_uri(uri);
printf("decoded_uri: %s\n", decoded_uri); struct evhttp_uri *decoded = NULL;
const char *path; /* Decode the URI */
decoded = evhttp_uri_parse(uri);
if (!decoded) {
printf("It's not a good URI. Sending BADREQUEST\n");
evhttp_send_error(req, HTTP_BADREQUEST, );
return;
} /* Let's see what path the user asked for. */
path = evhttp_uri_get_path(decoded);
if (!path) path = "/";
printf("path: %s\n", path); struct evbuffer *evb = evbuffer_new(); evbuffer_add_printf(evb, "Server response");
evhttp_send_reply(req, HTTP_OK, "OK", evb); evbuffer_free(evb);
} void print_request(struct evhttp_request *req, void *arg) {
const char *cmdtype;
struct evkeyvalq *headers;
struct evkeyval *header;
struct evbuffer *buf; switch (evhttp_request_get_command(req)) {
case EVHTTP_REQ_GET: cmdtype = "GET"; break;
case EVHTTP_REQ_POST: cmdtype = "POST"; break;
case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
default: cmdtype = "unknown"; break;
} printf("Received a %s request for %s\nHeaders:\n",
cmdtype, evhttp_request_get_uri(req)); headers = evhttp_request_get_input_headers(req);
for (header = headers->tqh_first; header;
header = header->next.tqe_next) {
printf(" %s: %s\n", header->key, header->value);
} buf = evhttp_request_get_input_buffer(req);
puts("Input data: <<<");
while (evbuffer_get_length(buf)) {
int n;
char cbuf[];
n = evbuffer_remove(buf, cbuf, sizeof(cbuf));
if (n > )
(void) fwrite(cbuf, , n, stdout);
}
puts(">>>");
}

libevent(六)http server的更多相关文章

  1. VMware vSphere服务器虚拟化实验六 vCenter Server 添加储存

                                                                          VMware vSphere服务器虚拟化实验六 vCente ...

  2. 基于Libevent的HTTP Server

    简单的Http Server 使用Libevent内置的http相关接口,可以很容易的构建一个Http Server,一个简单的Http Server如下: #include <event2/e ...

  3. libevent(六)事件监听

    libevent是如何实现事件监听的呢? 在Linux,libevent的底层实现是epoll,因此实现事件监听的方式就是,把需要监听的fd加入epoll中. I/O事件 定时器事件 定时器事件没有f ...

  4. libevent(十三)evhttp事件处理流程

    在libevent(六)http server中,作为一个单线程http server,不仅要监听每个连接的到来,还要监听每个连接上的I/O事件. 查看源码可知,在evhttp_bind_socket ...

  5. libevent(九)evhttp

    用libevent构建一个http server非常方便,可参考libevent(六)http server. 主要涉及的一个结构体是evhttp: struct evhttp { /* Next v ...

  6. libevent和libcurl实现http和https服务器 cJSON使用

    前言 libevent和libcurl都是功能强大的开源库:libevent主要实现服务器,包含了select.epoll等高并发的实现:libcurl实现了curl命令的API封装,主要作为客户端. ...

  7. 转:sql server锁知识及锁应用

    sql server锁(lock)知识及锁应用 提示:这里所摘抄的关于锁的知识有的是不同sql server版本的,对应于特定版本时会有问题. 一 关于锁的基础知识 (一). 为什么要引入锁 当多个用 ...

  8. SQL SERVER锁(LOCK)知识及锁应用

    提示:这里所摘抄的关于锁的知识有的是不同sql server版本的,对应于特定版本时会有问题. 一 关于锁的基础知识 (一). 为什么要引入锁 当多个用户同时对数据库的并发操作时会带来以下数据不一致的 ...

  9. ASP.NET常用内置对象之——Server

    简介 Server对象是HttpServerUtility的一个实例,也是上下文对象context的一个属性,提供用于处理Web请求的Helper方法. 常用成员 一.Server.MapPath() ...

随机推荐

  1. tf.nn.depthwise_conv2d 卷积

    tf.nn.depthwise_conv2d( input, filter, strides, padding, rate=None, name=None, data_format=None ) 参数 ...

  2. Retrofit 网络访问框架简单使用

    1.引入远程依赖:包括okhttp;retrofit2;retrofit的GSON解析器 compile'com.squareup.okhttp3:okhttp:3.2.0' compile'com. ...

  3. 计算机视觉中的对象检测,Python用几段代码就能实现

    目前计算机视觉(CV)与自然语言处理(NLP)及语音识别并列为人工智能三大热点方向,而计算机视觉中的对象检测(objectdetection)应用非常广泛,比如自动驾驶.视频监控.工业质检.医疗诊断等 ...

  4. Eight HDU - 1043 (双向BFS)

    记得上人工智能课的时候老师讲过一个A*算法,计算估价函数(f[n]=h[n]+g[n])什么的,感觉不是很好理解,百度上好多都是用逆向BFS写的,我理解的逆向BFS应该是从终点状态出发,然后把每一种状 ...

  5. C - Trailing Zeroes (III) 二分

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  6. string 中的getline

    1 getline 读入string库中的字符串 string a; getline(cin,a);  这样的读入要比任何一种读入字符串都有要快 2 char a[N]; cin.getline(a, ...

  7. Linux学习笔记(三)目录和文件都能操作的命令

    目录和文件都能操作的命令 rm cp mv rm 英文原意:remove files or directories 功能:删除文件或目录 语法:rm 选项[-fir] 文件或目录 rm -f 强制删除 ...

  8. SpringCloud(六)学习笔记之Zuul

    Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门 Hystrix+Ribbon(不使用Feign) ...

  9. Java中什么是构造方法

    this(...)本类的构造方法super(...)父类的构造方法构造方法:给对象的数据进行初始化格式:A:方法名与类名相同B:没有返回值类型,连void都没有C:没有具体的返回值注意事项:A:如果我 ...

  10. Metasploit学习笔记(一)

    1.更新 apt-get update:更新源 apt-get upgrade:更新软件包 apt-get dist-upgrade:升级系统 2. Metasploit基础 2.1专业名词 Auxi ...