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() ...
随机推荐
- 学习《深入应用c++11》2
&& universal references(未定的引用类型),它必须被初始化,它是左值还是右值取决于它的初始化,如果&&被一个左值初始化,它就是一个左值;如果它 ...
- 解决xcode ***is missing from working copy
这是由于SVN置顶文件导致的,cd 至项目根目录 命令行 输入 find . -type d -name .svn | xargs rm -rf
- 【论文笔记】张航和李沐等提出:ResNeSt: Split-Attention Networks(ResNet改进版本)
github地址:https://github.com/zhanghang1989/ResNeSt 论文地址:https://hangzhang.org/files/resnest.pdf 核心就是: ...
- Daily Scrum 12/23/2015
Process: Zhaoyang: Compile the Caffe IOS version and make it run in the IOS9. Yandong: Finish the Az ...
- icepdf和pdfbox转pdf文档为图片
icepdf转pdf文档为图片 首先导入icepdf jar包或maven pdfPath为pdf文件路径.pdfimgpsth为图片保存的路径 public static void icePdfIm ...
- 视频+图文 String类干货向总结
目录 一.字符串的介绍及视频讲解 二.字符串的两种创建方式 方法一:通过new创建 方法二:直接创建 三.字符串的长度获取:length()方法 四.使用 == 和equals()方法比较两个字符串 ...
- [PHP] 生成二维码(两种方法)
方法一:(调用google二维码接口,本人测试网不好,不好用!) <?php //1.封装生成二维码图片的函数(方法) /** *利用google api生成二维码图片 * $content:二 ...
- 人体和电脑的关系——鸟哥的LINUX私房菜基础学习篇读书笔记
CUP=脑袋: 每个人会做的事情都不一样(指令集的差异),但主要都是通过脑袋来判断与控制身体各部分的行动 内存=脑袋中存放正在思考的数据区块: 在实际活动过程中,我们的脑袋需要有外界刺激的数据(例如光 ...
- 常见分布式全局唯一ID生成策略
全局唯一的 ID 几乎是所有系统都会遇到的刚需.这个 id 在搜索, 存储数据, 加快检索速度 等等很多方面都有着重要的意义.工业上有多种策略来获取这个全局唯一的id,针对常见的几种场景,我在这里进行 ...
- Java 多线程--ThreadLocal Timer ExecutorService
ThreadLocal /** * ThreadLocal:每个线程自身的存储本地.局部区域 * @author xzlf * */ public class ThreadLocalTest01 { ...