HTML Forms, CGI och HTTP
Översikt Innehåll – Presentation – Beteende HTML Forms (innehåll) CGI (beteende) HTTP (beteende)
Webben i bild Webbläsare HTML Webbservrar
HTML-formulär
Forms Tutorials forms.htmlhttp:// forms.html
CGI i praktiken
The Common Gateway Interface (CGI) The task of a web server is to respond to requests from client web browsers by returning output. Each time a request is received, the server analyzes what the request asks for, and returns the appropriate output. The two simplest ways, for the server, of doing this are the following: –if the request identifies a file stored on disk, return the contents of that file; –if the request identifies an executable command and possibly arguments, run the command and return its output CGI defines a standard way of doing the second.
Palindrom-exempel: HTML Palindrome Checker Palindrome Checker Enter a word and find out if it's palindrome:
Palindrom-exempel : Python #!/usr/local/bin/python import cgi form = cgi.FieldStorage() word = form["word"].value print "Content-type: text/html\n\n" print " " if word == word[::-1]: print " Yes, %s is a palindrome " %(word,) else: print " No, %s is not a palindrome " %(word,) print " "
Palindrom-exempel: Prolog #!/usr/local/bin/plcon -q -g main -s :- use_module(library(cgi)). main :- cgi_get_form(Arguments), member(word(Atom),Arguments), atom_chars(Atom,Charlist), format('Content-type: text/html~n~n', []), format(" "), ( reverse(Charlist,Charlist) -> format(" Yes, ~w is a palindrome! ",[Atom]) ; format(" No, ~w is not a palindrome... ",[Atom]) ), format(" "), halt.
Installation av CGI-skript på HAL Ditt CGI-skript installerar du så här: 1.Skapa ett underbibliotek 'cgi-bin' i ditt www-bibliotek. 2.Placera palindrome.py (eller palindrome.pl) i detta bibliotek. 3.Byt namnet på filen till 'palindrome.cgi'. 4.Kör kommandot chmod a+rx palindrome.cgi för att sätta nödvändiga läs- och skrivrättigheter. 5.Ändra värdet på attributet ’action’ i elementet ’form’ i filen ”palindrome.html” så att det passar din installation. 6.Logga in på för att aktivera ditt skript. 7.Provkör!
Ditt www-bibliotek www/ index.html css/ main.css cgi-bin/ palindrome.cgi kurser/ webbteknologi/ index.html...
Provkörning file://C:/www/kurser/Webteknologi/webtechnology/cgi_test/palindrome.html
HTTP
Hypertext Transfer Protocol Webbens kommunikationsprotokoll Exempel: Från läsaren till servern (request): GET 2004/Talks/0914-tbl-speech/text HTTP/1.1 Från servern till läsaren (response): HTTP/ OK … Webbläsare HTML Webbservrar
Palindrom-exemplet igen… Testa i Firefox: Inspektera requests och responses m.h.a. verktyget LiveHTTPHeaders
HTTP request message GET /cgi-bin/palindrome.py?word=apa HTTP/1.1 Host: localhost User-Agent: Mozilla/5.0 (Windows; U; … Accept: text/xml,application/xml,... Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO ,utf-8… Keep-Alive: 300 Connection: keep-alive
HTTP request message GET /cgi-bin/palindrome.py?word=apa HTTP/1.1 Host: localhost User-Agent: Mozilla/5.0 (Windows; U; … Accept: text/xml,application/xml,... Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO ,utf-8… Keep-Alive: 300 Connection: keep-alive Data (if POST is used) request line (GET, POST,… commands) request headers extra carriage return, line feed) query
More about the query I webbläsaren: Request (en del av den): GET /cgi-bin/palindrome.py?word=apa HTTP/1.1 I CGI-skriptet –Hanteras oftast av ett bibliotek (library) och parsas och översätts där till en datastruktur där informationen blir lättåtkomlig
More about the query (cont’d) Ett sådan bibliotek har en del att göra: &client=firefox 8&aq=t&rls=org.mozilla:en-US:official&client=firefox Det som webbläsaren gör här kallas för “url encoding” Så CGI programmet måste alltså utföra “url decoding” Tur att man sällan behöver bry sig!!
HTTP response message HTTP/1.x 200 OK Date: Mon, 18 Feb :08:20 GMT Server: Apache/ (Win32) Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/html Yes, apa is a palindrome
HTTP response message HTTP/1.x 200 OK Date: Mon, 18 Feb :08:20 GMT Server: Apache/ (Win32) Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/html Yes, apa is a palindrome status line (protocol, status code, status phrase) response headers data
HTTP response status codes A few sample codes: –200 OK request succeeded, requested object later in this message –404 Not Found requested document not found on this server –500 Internal Server Error something is wrong with the server, or (more likely) with your CGI script
Laborationen