webserver example

108 views
Skip to first unread message

edward sillador

unread,
Mar 22, 2015, 11:34:24 PM3/22/15
to land-o...@googlegroups.com
I tried the solution from the following links to make the web server work but unfortunately it still does not work.

https://groups.google.com/forum/#!searchin/land-of-lisp/server/land-of-lisp/afwjs0eic94/nnnoJiVDkYAJ
https://groups.google.com/forum/?fromgroups=#!topic/land-of-lisp/Ei_Cragfbp0

Here is my modified version of hello-request-handler:
(defun hello-request-handler (path header params)
  (if (equal path "greeting")
      (let ((name (assoc 'name params)))
        (if (not name)
            (progn
              (princ "HTTP/1.1 200 OK~a")
              (princ "<html><form>What is your name?<input name='name' /> </form></html>"))
          (format t "<html>Nice to meet you, ~a!</html>" (cdr name))))
    (princ "Sorry... I don't know that page.")))

Could someone help me?

Thank you.

Simon Nicolussi

unread,
Mar 30, 2015, 6:05:07 PM3/30/15
to land-o...@googlegroups.com
Edward Sillador modified the hello-request-handler function:
> (if (not name)
> (progn
> (princ "HTTP/1.1 200 OK~a")
> (princ "<html><form>What is your name?<input name='name' />
> </form></html>"))
> (format t "<html>Nice to meet you, ~a!</html>" (cdr name))))

The hello-request-handler function writes to standard output, so you can
call it in the REPL to see whether its output matches your expectations:
> CL-USER> (hello-request-handler "greeting" nil nil)
> HTTP/1.1 200 OK~a<html><form>What is your name?<input name='name' /></form></html>

It probably doesn't, but it's unclear what exactly you expected. Control
sequences like ~a only take on a special meaning in control strings, the
second argument to the format function. Your function call, however, has
no value argument either (which is understandable, as princ would fail).

An HTTP status line has to end in CRLF (carriage return plus line feed),
followed by a header section (which is empty here), followed by an empty
line (another CRLF), followed by a message body (an HTML document). But
the book mentions that modern web browsers are forgiving; they accept a
single LF for EOL (as they're entitled to, according to RFC 7230, 3.5),
which means that ~% suffices as the line terminator on any of the major
operating systems. This leaves us with a function like the following:

(defun hello-request-handler (path header params)
(if (equal path "greeting")
(let ((name (assoc 'name params)))
(format t "HTTP/1.0 200 OK~2%")
(if (not name)
(princ "<html><form>What is your name?<input name='name' />
</form></html>")
(format t "<html>Nice to meet you, ~a!</html>" (cdr name))))
(princ "Sorry... I don't know that page.")))

As you can see, I moved the status line so that it precedes either HTML
document. Tested with Internet Explorer, Firefox, Chrome, Safari, Opera.

--
Simon Nicolussi <si...@sinic.name>
http{s,}://{www.,}sinic.name/
Reply all
Reply to author
Forward
0 new messages