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

從不浪費時間的人,沒有工夫抱怨時間不夠。 —— 杰弗遜

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

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.

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

翻譯過來,總結一下大致如下,

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

下載安裝

需要的依賴

使用步驟

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

代碼實現(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()

服務端

最簡代碼

#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設計開發(fā)的初衷,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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

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