热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

libevent(六)httpserver

客户端:#include#include#include#include#inc

客户端:

#include
#include

#include
<string.h>
#include

#include
#include event.h>
#include

#include

#include

#include
#define DEFAULT_URL "http://127.0.0.1:8080"void http_request_done(struct evhttp_request *req, void *arg){char buffer[256];int nread;if (req &#61;&#61; 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 &#61; evbuffer_remove(evhttp_request_get_input_buffer(req),buffer, sizeof(buffer)))> 0) {fwrite(buffer, nread, 1, stdout);}
}
int main(int argc, char **argv){char *url &#61; DEFAULT_URL;if (argc &#61;&#61; 2) {url &#61; argv[1];}struct evhttp_uri *http_uri &#61; NULL;const char *scheme, *host, *path, *query;char uri[256];int port;http_uri &#61; evhttp_uri_parse(url);scheme &#61; evhttp_uri_get_scheme(http_uri);if (scheme &#61;&#61; NULL || (strcasecmp(scheme, "https") !&#61; 0 &&strcasecmp(scheme, "http") !&#61; 0)) {printf("url must be http or https\n");return 1;}host &#61; evhttp_uri_get_host(http_uri);port &#61; evhttp_uri_get_port(http_uri);if (port &#61;&#61; -1) {port &#61; (strcasecmp(scheme, "http") &#61;&#61; 0) ? 80 : 443;}path &#61; evhttp_uri_get_path(http_uri);if (strlen(path) &#61;&#61; 0) {path &#61; "/";}query &#61; evhttp_uri_get_query(http_uri);if (query &#61;&#61; NULL) {snprintf(uri, sizeof(uri) - 1, "%s", path);} else {snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query);}uri[sizeof(uri) - 1] &#61; &#39;\0&#39;;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 &#61; event_base_new();conn &#61; evhttp_connection_base_new(base, NULL, host, port);req &#61; 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 0;
}

服务器&#xff1a;

#include
#include

#include

#include

#include
event.h>
#include
#define DEFAULT_PORT 8080void 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 &#61; DEFAULT_PORT;if (argc &#61;&#61; 2) {port &#61; atoi(argv[1]);}struct event_base *base &#61; event_base_new();struct evhttp *http &#61; 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 0;
}
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 &#61; evhttp_request_get_uri(req);printf("uri: %s\n", uri);const char *decoded_uri &#61; evhttp_decode_uri(uri);printf("decoded_uri: %s\n", decoded_uri);struct evhttp_uri *decoded &#61; NULL;const char *path;/* Decode the URI */decoded &#61; evhttp_uri_parse(uri);if (!decoded) {printf("It&#39;s not a good URI. Sending BADREQUEST\n");evhttp_send_error(req, HTTP_BADREQUEST, 0);return;}/* Let&#39;s see what path the user asked for. */path &#61; evhttp_uri_get_path(decoded);if (!path) path &#61; "/";printf("path: %s\n", path);struct evbuffer *evb &#61; 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 &#61; "GET"; break;case EVHTTP_REQ_POST: cmdtype &#61; "POST"; break;case EVHTTP_REQ_HEAD: cmdtype &#61; "HEAD"; break;case EVHTTP_REQ_PUT: cmdtype &#61; "PUT"; break;case EVHTTP_REQ_DELETE: cmdtype &#61; "DELETE"; break;case EVHTTP_REQ_OPTIONS: cmdtype &#61; "OPTIONS"; break;case EVHTTP_REQ_TRACE: cmdtype &#61; "TRACE"; break;case EVHTTP_REQ_CONNECT: cmdtype &#61; "CONNECT"; break;case EVHTTP_REQ_PATCH: cmdtype &#61; "PATCH"; break;default: cmdtype &#61; "unknown"; break;}printf("Received a %s request for %s\nHeaders:\n",cmdtype, evhttp_request_get_uri(req));headers &#61; evhttp_request_get_input_headers(req);for (header &#61; headers->tqh_first; header;header &#61; header->next.tqe_next) {printf(" %s: %s\n", header->key, header->value);}buf &#61; evhttp_request_get_input_buffer(req);puts("Input data: <<<");while (evbuffer_get_length(buf)) {int n;char cbuf[128];n &#61; evbuffer_remove(buf, cbuf, sizeof(cbuf));if (n > 0)(void) fwrite(cbuf, 1, n, stdout);}puts(">>>");
}

 

转:https://www.cnblogs.com/gattaca/p/6635869.html



推荐阅读
  • 本文深入探讨了HTTP请求和响应对象的使用,详细介绍了如何通过响应对象向客户端发送数据、处理中文乱码问题以及常见的HTTP状态码。此外,还涵盖了文件下载、请求重定向、请求转发等高级功能。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何在BackTrack 5中配置和启动SSH服务,确保其正常运行,并通过Windows系统成功连接。涵盖了必要的密钥生成步骤及常见问题解决方法。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 深入探讨CPU虚拟化与KVM内存管理
    本文详细介绍了现代服务器架构中的CPU虚拟化技术,包括SMP、NUMA和MPP三种多处理器结构,并深入探讨了KVM的内存虚拟化机制。通过对比不同架构的特点和应用场景,帮助读者理解如何选择最适合的架构以优化性能。 ... [详细]
  • 本文详细介绍如何使用arm-eabi-gdb调试Android平台上的C/C++程序。通过具体步骤和实用技巧,帮助开发者更高效地进行调试工作。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 利用存储过程构建年度日历表的详细指南
    本文将介绍如何使用SQL存储过程创建一个完整的年度日历表。通过实例演示,帮助读者掌握存储过程的应用技巧,并提供详细的代码解析和执行步骤。 ... [详细]
author-avatar
手机用户2502884057
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有