從不浪費時間的人,沒有工夫抱怨時間不夠。 —— 杰弗遜
本來是打算寫一個入門的介紹使用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ù)返回客戶端很困難。
- 缺少文檔,很難上手使用。
下載安裝
需要的依賴
使用步驟
- 創(chuàng)建一個 parent evhtp_t 結構。
- Assign callbacks to the parent for specific URIs or posix-regex based URI's(設置特定的URIs)。
- (可選)為回調函數(shù)聲明連接前的hooks。
- (可選)聲明連接前、后的回調函數(shù)。
- (可選)開啟內置的線程池處理連接。
- (可選)設置服務類型為HTTPS方式。
- 開始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服務的方法。