Libevhtp小試牛刀——使用Libevhtp實(shí)現(xiàn)http服務(wù)

從不浪費(fèi)時(shí)間的人,沒有工夫抱怨時(shí)間不夠。 —— 杰弗遜

本來是打算寫一個(gè)入門的介紹使用Libevhtp的小文章和大家分享,發(fā)現(xiàn)官方的文檔已經(jīng)寫的很好了,就翻譯了一下。但是限于自己能力有限,翻譯的不是很好,大家可以對(duì)應(yīng)著官方的介紹看。對(duì)于英語比較好的建議還是直接看英文吧。

LOGO
Libevhtp

Libevhtp簡(jiǎn)介

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.

簡(jiǎn)而言之,作者之所以開發(fā)這個(gè)基于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.

翻譯過來,總結(jié)一下大致如下,

  • 不是為全功能的HTTP服務(wù)而設(shè)計(jì)。
  • 代碼有點(diǎn)糟糕,幾乎沒有抽象,很難維護(hù)。
  • 解析數(shù)據(jù)慢,而且需要數(shù)據(jù)緩存之后才能完成完整解析。
  • 很難創(chuàng)建HTTP服務(wù)。
  • 流數(shù)據(jù)返回客戶端很困難。
  • 缺少文檔,很難上手使用。

下載安裝

需要的依賴

使用步驟

  1. 創(chuàng)建一個(gè) parent evhtp_t 結(jié)構(gòu)。
  2. Assign callbacks to the parent for specific URIs or posix-regex based URI's(設(shè)置特定的URIs)。
  3. (可選)為回調(diào)函數(shù)聲明連接前的hooks。
  4. (可選)聲明連接前、后的回調(diào)函數(shù)。
  5. (可選)開啟內(nèi)置的線程池處理連接。
  6. (可選)設(shè)置服務(wù)類型為HTTPS方式。
  7. 開始evhtp服務(wù)監(jiān)聽。

代碼實(shí)現(xiàn)

客戶端

#!/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()

服務(wù)端

最簡(jiǎn)代碼

#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;
}

總結(jié)

通過學(xué)習(xí),對(duì)Libevhtp有了一個(gè)簡(jiǎn)單的認(rèn)識(shí),學(xué)習(xí)到了Libevhtp設(shè)計(jì)開發(fā)的初衷,Libevhtp在Linux上下載、安裝的方法,書寫一個(gè)最簡(jiǎn)單的Libevhtp服務(wù)的方法。

參考文獻(xiàn)

  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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,934評(píng)論 0 23
  • 我們常常講減肥,但什么是減肥呢?很多人對(duì)于減肥的理解沒有弄清楚,就開始減減減,之后很快又胖胖胖。 我們減肥的目的是...
    健身帝閱讀 3,159評(píng)論 0 0
  • 現(xiàn)在的我哈欠連天的坐在電腦前,開始寫下今天的文字。昨晚十點(diǎn)半就睡了,這對(duì)我來說簡(jiǎn)直是個(gè)天大的改變,我也因此能夠成功...
    適說心語閱讀 219評(píng)論 0 0
  • 把蟑螂打死,她就不害怕了。人總是要從擊敗和傷害中找到自豪感和安全感。 (1) 周迅在電視上用不男不女的聲音無辜地說...
    定君閱讀 397評(píng)論 3 1