HTTP 協議解析庫:fast-http

jopen 10年前發布 | 118K 次閱讀 fast-http Lisp開發

這是一個快速的 HTTP request/response 協議解析器,用于 Common Lisp開發中。 大部分移植自 C 語言版本的 http-parser.

具體有多快請看下圖:

HTTP 協議解析庫:fast-http

NOTE: Deleted PicoHTTPParser because the benchmark was wrong. It's 3.7 times faster than fast-http. Amazing.

詳情請看 Benchmark

用法

其API非常類似于 http-parse.

(let* ((http (make-http-request))
       (parser (make-parser http
                            :header-callback (lambda (headers)
                                               (my-app:got-headers!!! headers))
                            :body-callback (lambda (bytes)
                                             (my-app:got-body-piece bytes)))))
  (loop for http-data = (my-app:get-http-data-from-request-i-sent-out-earlier) do
    (multiple-value-bind (http headers-finished-p body-finished-p)
        (funcall parser http-data)
      (when body-finished-p
        (my-app:close-http-stream))
      ...)))

API 與 http-parse 不同之處

  • http, http-request and http-response are structure classes, not standard classes.
  • http doesn't have :force-stream option. (always streaming)
  • http doesn't have :store-body option because it can consume much memory.
  • body-callback for make-parser and make-multipart-parser doesn't take a flag body-complete-p.
    • Use finish-callback to know if the parsing is finished.
  • :multipart-callback of make-parser and :callback of make-multipart-parser takes a stream, not a body octet vector at the 4th argument.
  • Raises errors aggressively while parsing.
    • Handle fast-http-error as you needed.
  • Doesn't use a property list as a representation of HTTP headers. (See issue #1)

項目主頁:http://www.baiduhome.net/lib/view/home/1414720724559

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!