Professional Documents
Culture Documents
Chapter 9
Chapter 9
M. L. Liu
req u es t is a m es s ag e in 3 p arts :
req u es t
- < c o m m an d > < d o c u m en t ad d d res s > < HT T P v ers io n >
- an o p tio n al h ead er
- o p tio n al d ata fo r C G I d ata u s in g p o s t m eth o d
Example2:
HEAD / HTTP/1.1
Accept: */*
Connection: Keep-Alive
Host: somehost.com
User-Agent: Generic
<blank line>
<html>
<head>
<title>My web page </title>
</head>
<body>
Hello world!
</BODY></HTML>
Application Octet-stream (can be used for transferring Java .class files, for example), Adobe-postscript, Mac-
binhex40, xml
audio basic,midi,mp3
mySocket.sendMessage(request);
// now receive the response from the HTTP server
String response = mySocket.receiveMessage();
// read and display one line at a time
while (response != null) {
System.out.println(response);
response = mySocket.receiveMessage();
}
URL(String spec) Creates a URL object from the URL name contained in a String
.
URL(String protocol,String host, Creates a URL object from the specified protocol, host, port
int port,String file) number, and file.
InputStream openStream( ) Opens a connection to this URL and returns an InputStream for
reading from that connection.
we b s e rv e r we b clie n t
"id = 1 2 3 4 5 "
"in c o m e= 3 0 0 0 0 "
" id = 1 2 3 4 5 "
" in c o m e = 3 0 0 0 0 "
da ta ba s e s y s te m
re qu e s t
re s pon s e
we b clie n t h o s t
HT T P s erv er
C G I s c rip t
w eb c lien t
req u es t fo r h ello .h tm l
c o n ten ts o f h ello .h tm l
req u es t fo r h ello .c g i
d ata, if an y , fro m th e c lien t
See:
getForm.html
getForm.c
HTTP/1.1 200 OK
Date: Sun, 24 Feb 2002 22:30:55 GMT
Server: Apache/1.3.9 (Unix) ApacheJServ/1.0
Connection: close
Content-Type: text/html
name=John%20Doe&quest=peace%20on%20earth&color=azure
Since the length of the request body is unlimited, the query string can
be of arbitrary length. Hence the POST method can be used to send
any amount of query data to the server.
01/13/24 Distributed Computing, M. L. Liu 69
Form POST method – server to external program
The server invokes the CGI script and passes on the query
string that it received from the browser via the request
body.
The CGI program, or the external program in general, will
receive the encoded form input on the standard input.
“The server will NOT send you an EOF on the end of the
data, instead you should use the environment variable
CONTENT_LENGTH to determine how much data you
should read from (the standard input).”
The CGI program reads the query string from the standard
input, decode the character string to obtain the name-value
pairs, and uses the parameters during the execution of the
program to generate output phrased in HTML.
See:
postForm.html
postForm.c
name=Donald
HTTP/1.1 200 OK
Date: Sun, 24 Feb 2002 22:52:33 GMT
Server: Apache/1.3.9 (Unix) ApacheJServ/1.0
Connection: close
Content-Type: text/html
fo rm .h tm l
fo rm .c g i?id = 1 2 3 4 5
id = 1 2 3 4 5
fo rm 2 .h tm l(d y n am ic )
fo rm 2 .h tm l (d y n am ic )
fo rm 2 .c g i?b u y = T V
buy=T V
"c u s to m er 1 2 3 4 5 h as a T V in s h o p p in g c art"
"c u s to m er 1 2 3 4 5 h as a T V in s h o p p in g c art"
C GI we b
we b clie n t
s cript 1 s e rv e r i d =1 2 3 4 5
C GI
s cript 2 i d =1 2 3 4 5
i d =1 2 3 4 5 & b u y =TV
C GI
s cript 3 i d =1 2 3 4 5 & b u y =TV
i d =1 2 3 4 5 & b u y - TV & c h a r g e =5 0 0 .3 5
...
< I N P U T n a m e = " Y o ur N a m e " >
Yo urnam e = J o hn B ro wn
fo r m 1 .c g i
...
< F O R M M E T H O D = P O ST A C T I O N = " /c gi- bin /f o r m 2 .c gi"
p ir n t f ( " < I N P U T n a m e = Y o ur I D > " ;
p r in t f ( " < I N P U T T Y P E = " H I D D E N " N A M E = % s VA L U E = % s> " , e n t r ie s[ 0 ] .n a m e , e n t r ie s[ 0 ] . v a l) ;
...
Yo urID = 1 2 3 4 5 & Yo urN am e = J o hn B ro wn
fo r m 2 .c g i
...
< F O R M M E T H O D = P O ST A C T I O N = " /c gi- bin /f o r m 3 .c gi"
p r in t f ( " < I N P U T T Y P E = " H I D D E N " N A M E = " Y o ur N a m e " VA L U E = " Jo h n B r o wn " > " ) ;
p r in t f ( " < I N P U T T Y P E = " H I D D E N " N A M E = % s VA L U E = % s> " , e n t r ie s[ 0 ] .n a m e , e n t r ie s[ 0 ] . v a l) ;
p r in t f ( " < I N P U T T Y P E = " H I D D E N " N A M E = % s VA L U E = % s> " , e n t r ie s[ 1 ] .n a m e , e n t r ie s[ 1 ] . v a l) ;
...
Yo urID = 1 2 3 4 5 & Yo urN am e = J o hn B ro wn
we b s cript
we b s cript W e b s e rv e r B ro ws e r
fo rm .cg i
fo rm 2 .cg i
r e que st f o r f o r m .h t m l
f o r m .h t m l
f o r m .c gi? id= 1 2 3 4 5
id= 1 2 3 4 5
f o r m 2 .h t m l c o n t a in s
< in p ut t y p e = h idde n n a m e = id v a lue = " l2 3 4 5 " >
f o r m 2 .h t m l ( dy n a m ic )
hiddenForm2.c
...
< I N P U T n a m e = " Y o ur N a m e " >
fo r m 1 .c g i B ro w s e r
... Yo u rn am e= J o h n Bro w n
< F O R M M E T H O D = P O ST A C T I O N = " /c gi- bin / f o r m 2 .c gi"
p rin tf ("s et-c o o k ie:% s = % s ", en tries [0 ].n am e, en tr ies [0 ]. v al);
...
Yo u rn am e= J o h n Bro w n
fo r m 2 .c g i
...
< F O R M M E T H O D = P O ST A C T I O N = " /c gi- bin / f o r m 3 .c gi"
Yo u rID = 1 2 3 4 5
p rin tf ("s et-c o o k ie:% s = % s % c \n ", en tries [0 ].n am e, en tries [0 ]. v al,'\0 ');
item 1 = b o o k 1 3 7 1 4
p rin tf ("s et-c o o k ie:% s = % s % c \n ", en tries [4 ].n am e, en tries [4 ]. v al,'\0 ');
...
fo r m 3 ,c g i
f o r m .c g i ? i d =1 2 3 4 5
i d =1 2 3 4 5
S e t - C o o k i e : i d =1 2 3 4 5
f o r m 2 .h t m l ( d y n a m i c )
f o r m 2 .h t m l ( d y n a m i c ) i d =1 2 3 4 5
f o r m 2 .c g i ? n a m e =M a t t
i d =1 2 3 4 5
i d =1 2 3 4 5 & n a m e =M a t t
S e t - C o o k i e : n a m e =M a t t f o r m 3 .h t m l ( d y n a m i c )
f o r m 3 .h t m l ( d y n a m i c ) n a m e =M a t t
i d =1 2 3 4 5 & n a m e =M a t t i d =1 2 3 4 5 n a m e =M a t t
Examples:
Cookie Domain Cookie Path Request URI that will cause the name-value pair in
attribute attribute the cookie to be sent in the request header.
www.someU.edu / www.someU.edu/*
Cookie/Cookie.html
Cookie/Cookie.c
Cookie/Cookie2.c