程序员Java服务器端编程

Libevhtp小试牛刀——使用Libevhtp实现http服务

2017-08-21  本文已影响0人  落落书生

从不浪费时间的人,没有工夫抱怨时间不够。 —— 杰弗逊

本来是打算写一个入门的介绍使用Libevhtp的小文章和大家分享,发现官方的文档已经写的很好了,就翻译了一下。但是限于自己能力有限,翻译的不是很好,大家可以对应着官方的介绍看。对于英语比较好的建议还是直接看英文吧。

LOGO Libevhtp

Libevhtp简介

Libevhtp was created as a replacement API for Libevent's current HTTP API. The reality of libevent's http interface is that it was created as a JIT server, meaning the developer never thought of it being used for creating a full-fledged HTTP service. Infact I am under the impression that the libevent http API was designed almost as an example of what you can do with libevent. It's not Apache in a box, but more and more developers are attempting to use it as so.

简而言之,作者之所以开发这个基于Libevent HTTP的库,是为了解决Libevent HTTP不太好用的特性,下面列举了一系列Libevent HTTP的瑕疵。

  • It was not designed to be a fully functional HTTP server.
  • The code is messy, abstractions are almost non-existent, and feature-creep has made long-term maintainability very hard.
  • The parsing code is slow and requires data to be buffered before a full parse can be completed. This results in extranious memory usage and lots of string comparison functions.
  • There is no method for a user to access various parts of the request processing cycle. For example if the "Content-Length" header has a value of 50000, your callback is not executed until all 50000 bytes have been read.
  • Setting callback URI's do exact matches; meaning if you set a callback for "/foo/", requests for "/foo/bar/" are ignored.
  • Creating an HTTPS server is hard, it requires a bunch of work to be done on the underlying bufferevents.
  • As far as I know, streaming data back to a client is hard, if not impossible without messing with underlying bufferevents.
  • It's confusing to work with, this is probably due to the lack of proper documentation.

翻译过来,总结一下大致如下,

下载安装

需要的依赖

使用步骤

  1. 创建一个 parent evhtp_t 结构。
  2. Assign callbacks to the parent for specific URIs or posix-regex based URI's(设置特定的URIs)。
  3. (可选)为回调函数声明连接前的hooks。
  4. (可选)声明连接前、后的回调函数。
  5. (可选)开启内置的线程池处理连接。
  6. (可选)设置服务类型为HTTPS方式。
  7. 开始evhtp服务监听。

代码实现

客户端

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import urllib
import urllib2
import time



def parseText():
    data_urlencode = urllib.urlencode("Hello World!")
    requrl = "http://127.0.0.1:8090/test/"
    req = urllib2.Request(url = requrl, data_urlencode)
    res_data = urllib2.urlopen(req)
    res = res_data.read()
    print res

def main():
        time1 = time.time()
        parseText()
        time2 = time.time()
        print time2 - time1

if __name__ == '__main__':
        main()

服务端

最简代码

#include <stdio.h>
#include <evhtp.h>

void
testcb(evhtp_request_t * req, void * a) {
    evbuffer_add_reference(req->buffer_out, "foobar", 6, NULL, NULL);
    evhtp_send_reply(req, EVHTP_RES_OK);
}

int
main(int argc, char ** argv) {
    evbase_t * evbase = event_base_new();
    evhtp_t  * htp    = evhtp_new(evbase, NULL);

    evhtp_set_cb(htp, "/test", testcb, NULL);
    evhtp_bind_socket(htp, "0.0.0.0", 8080, 1024);
    event_base_loop(evbase, 0);
    return 0;
}

总结

通过学习,对Libevhtp有了一个简单的认识,学习到了Libevhtp设计开发的初衷,Libevhtp在Linux上下载、安装的方法,书写一个最简单的Libevhtp服务的方法。

参考文献

  1. https://github.com/criticalstack/libevhtp
  2. http://blog.chinaunix.net/uid-24567872-id-3860909.html
  3. http://www.ruanyifeng.com/blog/2015/02/make.html
上一篇下一篇

猜你喜欢

热点阅读