HTTP 协议解析库:fast-http

jopen 9年前

这是一个快速的 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.open-open.com/lib/view/home/1414720724559