1

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.

5
  • Oh, normal web pages like www.google.com have a % at the end of the output too when I do command-line curl is OS X, so I guess that fact isn't relevant. Well, google does, but microsoft.com doesn't. Not sure where it comes from. Commented Apr 4, 2019 at 23:03
  • 1
    I think this is related to your problem groups.google.com/forum/#!topic/land-of-lisp/afwjs0eic94 Commented Apr 5, 2019 at 14:57
  • @MartinBuchmann: for more conformance write CR LF at the end of a line. For security: never call read-from-string without *read-eval* being nil. Commented Apr 5, 2019 at 21:02
  • Hmm.. I tried adding (format t "HTTP/1.1 200 OK~%") before the other output in a progn block, but it didn't fix anything. Commented Apr 6, 2019 at 2:57
  • If you are on UNIX ~% will write a linefeed. The end of a HTTP line is cr and then lf. Not just lf alone. Write a return character and then a linefeed character. Commented Apr 6, 2019 at 5:44

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.