Skip to content

Instantly share code, notes, and snippets.

@llibra
Created September 11, 2012 20:30
Show Gist options
  • Save llibra/3701770 to your computer and use it in GitHub Desktop.
Save llibra/3701770 to your computer and use it in GitHub Desktop.
DRAKMA:HTTP-REQUEST drops some input characters
(ql:quickload :cl-ppcre)
(ql:quickload :bordeaux-threads)
(ql:quickload :usocket)
(ql:quickload :drakma)
(asdf:component-version (asdf:find-system :drakma))
;=> "1.2.7"
(flet ((echo-content ()
(usocket:with-socket-listener (socket "127.0.0.1" 8080 :reuse-address t
:element-type '(unsigned-byte 8))
(let ((socket (usocket:socket-accept socket)))
(with-open-stream (stream (usocket:socket-stream socket))
(let ((stream (flexi-streams:make-flexi-stream stream)))
(read-line stream) ; Skip Request-Line
(read-line stream) ; Skip Host
(read-line stream) ; Skip User-Agent
(read-line stream) ; Skip Accept
(read-line stream) ; Skip Connection
(read-line stream) ; Skip Content-Type
(multiple-value-bind (_ regs)
(ppcre:scan-to-strings "^Content-Length: (\\d+)" (read-line stream))
(declare (ignore _))
(let ((length (read-from-string (aref regs 0)))
(crlf (format nil "~c~c" #\return #\linefeed)))
(read-line stream) ; Skip CRLF
(format stream "HTTP/1.1 200 OK~a" crlf)
(format stream "Content-Type: text/plain; charset=UTF-8~a" crlf)
(format stream "Content-Length: ~a~a" length crlf)
(format stream "~a" crlf)
(dotimes (_ length)
(write-byte (read-byte stream) stream))
(force-output stream)))))))))
(bordeaux-threads:make-thread #'echo-content))
(let ((drakma:*drakma-default-external-format* :utf-8))
(drakma:http-request "http://127.0.0.1:8080"
:method :put
:content-type "text/plain; charset=UTF-8"
:content-length t
:content "日本語"))
;=> "日",
; 200,
; ((:CONTENT-TYPE . "text/plain; charset=UTF-8") (:CONTENT-LENGTH . "3")),
; #<URI http://127.0.0.1:8080/>,
; #<FLEXI-STREAMS:FLEXI-IO-STREAM #x192793C6>,
; T,
; "OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment