Lewis Cawthorne
Lewis Cawthorne

Reputation: 55

Getting ERR_INVALID_HTTP_RESPONSE using web server code in Land of Lisp with clisp on OS X

I'm reading Land of Lisp (it's 10 years old, but so is clisp, so it seems up to date enough). I'm on Chapter 13, where you write a web server. It just opens the client socket as standard-output, then uses princ and format t to write out to the client. The full source code of the web server is at http://landoflisp.com/webserver.lisp.

I'm having the same problem with both my typed in code and downloading the webserver.lisp from the website and running it. When I go to the site in Chrome, I get ERR_INVALID_HTTP_RESPONSE. In Safari I get can't open page. When I try to go to the site in Firefox, I get the expected page, but all of the html tags are just rendered as text. When I try interacting with the site in curl, I'm getting a closing % in the output that I'm not sure where it's coming from.

> curl http://localhost:8080/greeting
<html><form>What is your name?<input name='name' /></form></html>%

I know it's not the best HTML, but it does the same if I add body tags and declare doctype html at the start too. The % in the curl response isn't there in the princ, and I get a % when I try going to other URL's for the server like localhost:8080 or localhost:8080/greeting?name=Lewis.

I don't actually plan to use this web server for much of anything, but the rest of the book seems involved in making an app that uses it, and I'ld like to finish the book, so it would be nice if I could at least get the web server working with at least one browser.

Upvotes: 1

Views: 1183

Answers (1)

Lewis Cawthorne
Lewis Cawthorne

Reputation: 55

Looks like this fixed it, as per the discussion above pointing to the forum. We needed an "HTTP 1.1 200 OK" followed by a \r\n, then a \r\n on a newline by itself, then the page. That got it working right in both Firefox and Chrome. I just added this to the request handler:

(format t "HTTP/1.1 200 OK~C~C" #\return #\linefeed)
(format t "~C~C" #\return #\linefeed)

Thanks for all the help in the discussion getting me to this answer!

Upvotes: 1

Related Questions