客户端:

#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. python3(二十七)property

    """ """ __author__ = 'shaozhiqi' # 绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单, # 但是, ...

  2. tcp协议:三次握手四次挥手详解-转

    https://www.cnblogs.com/welan/p/9925119.html

  3. Linux Mint(Ubuntu)如何管理开机自动启动项?

    Linux Mint自带了一个简洁的开机自启管理应用,使用方法也很简单: 依次点击“Menu”==>“控制中心”==>“个人”==>“启动应用程序”,界面如图所示: 上面打勾的就是系 ...

  4. python3+selenium3自动化1——元素定位

    1.selenium的webdriver提供了八种基本的元素定位方法 打开浏览器 driver = webdriver.Chrome() driver.get('https://www.baidu.c ...

  5. 图解Knative核心组件Serving基础设计

    最近闲下来,打算把Knative的核心组件Serving给学习下,会继续采用k8s源码学习的方式,管中窥豹以小击大,学习serving的主要目标: 可观测性基础设施.自动伸缩.流量管理等核心组件的设计 ...

  6. Java 使用正则表达式和IO实现爬虫以及503解决

    我这边找了个小说网站: 基本套路: 第一步:获取小说每一章的url地址 第二步:获取章节url内容并使用正则表达式提取需要的内容 第三步:多线程封装,实现如下效果 最后测试. 代码: 内容获取封装: ...

  7. thinkphp5 -- _initialize()初始化控制器

    public function _initialize() { parent::_initialize(); } public function __construct() { $; parent:: ...

  8. 详解 Discuz 的 PHP经典加密解密函数 authcode

    函数注释: // $string: 明文 或 密文 // $operation:DECODE表示解密,其它表示加密 // $key: 密匙 // $expiry:密文有效期 function auth ...

  9. QMessage自动定时关闭

    QMessageBox *box = new QMessageBox(QMessageBox::Information,tr("test"),tr("testtest&q ...

  10. CentOS上安装配置 mongodb

    CentOS 首先yum list mongo* 查看是否有关于mongo的安装包,检查后安装即可   mongo 分client端和server端,server启动db服务,client可以连接到s ...