libevent(六)http server
客户端:
#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的更多相关文章
- VMware vSphere服务器虚拟化实验六 vCenter Server 添加储存
VMware vSphere服务器虚拟化实验六 vCente ...
- 基于Libevent的HTTP Server
简单的Http Server 使用Libevent内置的http相关接口,可以很容易的构建一个Http Server,一个简单的Http Server如下: #include <event2/e ...
- libevent(六)事件监听
libevent是如何实现事件监听的呢? 在Linux,libevent的底层实现是epoll,因此实现事件监听的方式就是,把需要监听的fd加入epoll中. I/O事件 定时器事件 定时器事件没有f ...
- libevent(十三)evhttp事件处理流程
在libevent(六)http server中,作为一个单线程http server,不仅要监听每个连接的到来,还要监听每个连接上的I/O事件. 查看源码可知,在evhttp_bind_socket ...
- libevent(九)evhttp
用libevent构建一个http server非常方便,可参考libevent(六)http server. 主要涉及的一个结构体是evhttp: struct evhttp { /* Next v ...
- libevent和libcurl实现http和https服务器 cJSON使用
前言 libevent和libcurl都是功能强大的开源库:libevent主要实现服务器,包含了select.epoll等高并发的实现:libcurl实现了curl命令的API封装,主要作为客户端. ...
- 转:sql server锁知识及锁应用
sql server锁(lock)知识及锁应用 提示:这里所摘抄的关于锁的知识有的是不同sql server版本的,对应于特定版本时会有问题. 一 关于锁的基础知识 (一). 为什么要引入锁 当多个用 ...
- SQL SERVER锁(LOCK)知识及锁应用
提示:这里所摘抄的关于锁的知识有的是不同sql server版本的,对应于特定版本时会有问题. 一 关于锁的基础知识 (一). 为什么要引入锁 当多个用户同时对数据库的并发操作时会带来以下数据不一致的 ...
- ASP.NET常用内置对象之——Server
简介 Server对象是HttpServerUtility的一个实例,也是上下文对象context的一个属性,提供用于处理Web请求的Helper方法. 常用成员 一.Server.MapPath() ...
随机推荐
- Python设计模式(11)-状态模式
# coding=utf-8 # *状态模式:一个方法的判断逻辑太长,就不容易修改.方法过长,其本质就是,# * 就是本类在不同条件下的状态转移.状态模式,就是将这些判断分开到各个能# * 表示当前状 ...
- 08-jmeter-plugins-manager.jar插件安装
1.安装第三方插件:jmeter-plugins-manager 2.将此jar包下载好后放到jmeter的安装目录lib/ext文件夹内, 3.然后运行jmeter,选择菜单“选项”可见插件入口 4 ...
- C语言小练习之学生信息管理系统
C语言小练习之学生信息管理系统 main.c文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...
- Python-selenium 元素定位
1.id定位find_element_by_id() 通过id属性定位元素,如果id是动态变化的话不能用id来进行定位 2.name定位find_element_by_name() 通过name属性定 ...
- Python中赋值、浅拷贝和深拷贝的区别
前言文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http: ...
- Python程序设计实验报告一:熟悉IDLE和在线编程平台
安徽工程大学 Python程序设计 实验报告 班级 物流191 姓名 崔攀 学号3190505136 成绩_____ 日期 2020.3.8 指导老师 ...
- 常用ElasticSearch 查询语句
为了演示不同类型的 ElasticSearch 的查询,我们将使用书文档信息的集合(有以下字段:title(标题), authors(作者), summary(摘要), publish_date(发布 ...
- 《并发编程的艺术》阅读笔记之Sychronized
概述 在JDK1.6中,锁一共四种状态,级别由低到高依次是:无锁状态.偏向锁状态.轻量级锁状态和重量级锁状态.锁可以升级但不能降级,这是为了提高获得锁和释放锁的效率.只有重量级锁涉及到操作系统线程切换 ...
- 关于ubuntu安装vmware报错问题解决
命令行中报错 首先报错内容为:(vmware-installer.py:3847): Gtk-WARNING **: 无法在模块路径中找到主题引擎:“murrine”, 以上的内容: sudo apt ...
- C#多线程(15):任务基础③
目录 TaskAwaiter 延续的另一种方法 另一种创建任务的方法 实现一个支持同步和异步任务的类型 Task.FromCanceled() 如何在内部取消任务 Yield 关键字 补充知识点 任务 ...