從不浪費時間的人晤柄,沒有工夫抱怨時間不夠傍念。 —— 杰弗遜
本來是打算寫一個入門的介紹使用Libevhtp的小文章和大家分享围辙,發(fā)現(xiàn)官方的文檔已經(jīng)寫的很好了吉捶,就翻譯了一下夺鲜。但是限于自己能力有限皆尔,翻譯的不是很好,大家可以對應(yī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.
翻譯過來仅胞,總結(jié)一下大致如下浪感,
- 不是為全功能的HTTP服務(wù)而設(shè)計。
- 代碼有點糟糕饼问,幾乎沒有抽象,很難維護(hù)揭斧。
- 解析數(shù)據(jù)慢莱革,而且需要數(shù)據(jù)緩存之后才能完成完整解析。
- 很難創(chuàng)建HTTP服務(wù)讹开。
- 流數(shù)據(jù)返回客戶端很困難盅视。
- 缺少文檔,很難上手使用旦万。
下載安裝
需要的依賴
使用步驟
- 創(chuàng)建一個 parent evhtp_t 結(jié)構(gòu)闹击。
- Assign callbacks to the parent for specific URIs or posix-regex based URI's(設(shè)置特定的URIs)。
- (可選)為回調(diào)函數(shù)聲明連接前的hooks成艘。
- (可選)聲明連接前赏半、后的回調(diào)函數(shù)。
- (可選)開啟內(nèi)置的線程池處理連接淆两。
- (可選)設(shè)置服務(wù)類型為HTTPS方式断箫。
- 開始evhtp服務(wù)監(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()
服務(wù)端
最簡代碼
#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í)秋冰,對Libevhtp有了一個簡單的認(rèn)識仲义,學(xué)習(xí)到了Libevhtp設(shè)計開發(fā)的初衷,Libevhtp在Linux上下載剑勾、安裝的方法埃撵,書寫一個最簡單的Libevhtp服務(wù)的方法。