Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird HTTP problem/mistake with Lisp
    text
    copied!<p>I'm attempting to learn a little more about handling sockets and network connections in SBCL; so I wrote a simple wrapper for HTTP. Thus far, it merely makes a stream and performs a request to ultimately get the header data and page content of a website.</p> <p>Until now, it has worked at somewhat decently. Nothing to brag home about, but it at least worked.</p> <p>I have come across a strange problem, however; I keep getting "400 Bad Request" errors.</p> <p>At first, I was somewhat leery about how I was processing the HTTP requests (more or less passing a request string as a function argument), then I made a function that formats a query string with all the parts I need and returns it for use later... but I still get errors.</p> <p>What's even more odd is that the errors don't happen every time. If I try the script on a page like Google, I get a "200 Ok" return value... but at other times on other sites, I'll get "400 Bad Request".</p> <p>I'm certain its a problem with my code, but I'll be damned if I know exactly what is causing it.</p> <p>Here is the code that I am working with:</p> <pre><code>(use-package :sb-bsd-sockets) (defun read-buf-nonblock (buffer stream) (let ((eof (gensym))) (do ((i 0 (1+ i)) (c (read-char stream nil eof) (read-char-no-hang stream nil eof))) ((or (&gt;= i (length buffer)) (not c) (eq c eof)) i) (setf (elt buffer i) c)))) (defun http-connect (host &amp;optional (port 80)) "Create I/O stream to given host on a specified port" (let ((socket (make-instance 'inet-socket :type :stream :protocol :tcp))) (socket-connect socket (car (host-ent-addresses (get-host-by-name host))) port) (let ((stream (socket-make-stream socket :input t :output t :buffering :none))) stream))) (defun http-request (stream request &amp;optional (buffer 1024)) "Perform HTTP request on a specified stream" (format stream "~a~%~%" request ) (let ((data (make-string buffer))) (setf data (subseq data 0 (read-buf-nonblock data stream))) (princ data) (&gt; (length data) 0))) (defun request (host request) "formated HTTP request" (format nil "~a HTTP/1.0 Host: ~a" request host)) (defun get-page (host &amp;optional (request "GET /")) "simple demo to get content of a page" (let ((stream (http-connect host))) (http-request stream (request host request))) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload