annotate test/cedet/tests/test.py @ 107467:f0ec21d379ff

* finder.el: Remove TODO tasks. * info.el (Info-finder-find-node): Add node "all" with all package info. Handle a list of multiple keywords separated by comma. (info-finder): In interactive use with a prefix argument, use `completing-read-multiple' to read a list of keywords separated by comma.
author Juri Linkov <juri@jurta.org>
date Tue, 23 Mar 2010 09:11:50 +0200
parents 9a13ceca63a8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
105267
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
1 # Test file for Python language.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
2 #
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
3
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
4 # Simle class compount statement with blank lines sprinkled.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
5 class Foo(Bar):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
6
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
7 x = 1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
8
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
9 y = 2
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
10
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
11 # Simple def statement with no argument
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
12 def sss():
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
13 i = 1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
14
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
15 # Simple def statement with arguments
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
16 def ttt(x,y,z):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
17 i = 1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
18
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
19 import foo
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
20
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
21 for x in y:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
22 print x
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
23
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
24 while y > 0:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
25 y = y - 1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
26
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
27 a=b=c=d=e=f=i=j=k=l=m=n=o=p=q=r=s=t=x=y=1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
28
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
29 if x:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
30 x = 2
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
31 y = 3
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
32
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
33 x = 2
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
34 y = 3
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
35 s and t
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
36 q | r
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
37 o ^ p
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
38 m & n
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
39 k << l
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
40 z = 4
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
41 i >> j
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
42 e / f
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
43 c * d
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
44 a + b
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
45 2 ** 5
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
46 x
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
47 s = "a" "b" "c"
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
48 1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
49
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
50 # implicit continuation lines, see
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
51 # http://docs.python.org/ref/implicit-joining.html
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
52
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
53 a_list = [ 1, 2, 3,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
54 4, 5,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
55 6 ]
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
56
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
57 a_tuple = (1, 2, 3,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
58
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
59 4, 5, 6)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
60
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
61 a_hash = { 'a':1, "b":2,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
62 'c' : 3,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
63 "d" : 4 }
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
64
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
65
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
66 def longarglist(a,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
67 b,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
68 c,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
69 d):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
70 a=1;
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
71 b=1;
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
72 c=1;
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
73 d=1;
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
74
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
75 class longclasslist(xx.yyy,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
76 zz.aa):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
77 foo=1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
78
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
79
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
80 # wisent-python.wy chokes on this! -ryk 6/17/02
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
81
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
82 class HTTPServer(xxx.yyy):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
83 allow_reuse_address = 1 # Seems to make sense in testing environment
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
84 def server_bind(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
85 SocketServer.TCPServer.server_bind(self)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
86 host, port = self.socket.getsockname()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
87 self.server_name = socket.getfqdn(host)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
88 self.server_port = port
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
89
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
90
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
91 #########################################################################
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
92 ### /usr/lib/python2.2/BaseHTTPServer.py
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
93 #########################################################################
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
94
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
95 """HTTP server base class.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
96
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
97 Note: the class in this module doesn't implement any HTTP request; see
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
98 SimpleHTTPServer for simple implementations of GET, HEAD and POST
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
99 (including CGI scripts).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
100
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
101 Contents:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
102
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
103 - BaseHTTPRequestHandler: HTTP request handler base class
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
104 - test: test function
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
105
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
106 XXX To do:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
107
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
108 - send server version
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
109 - log requests even later (to capture byte count)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
110 - log user-agent header and other interesting goodies
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
111 - send error log to separate file
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
112 - are request names really case sensitive?
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
113
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
114 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
115
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
116
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
117 # See also:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
118 #
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
119 # HTTP Working Group T. Berners-Lee
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
120 # INTERNET-DRAFT R. T. Fielding
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
121 # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
122 # Expires September 8, 1995 March 8, 1995
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
123 #
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
124 # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
125
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
126
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
127 # Log files
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
128 # ---------
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
129 #
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
130 # Here's a quote from the NCSA httpd docs about log file format.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
131 #
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
132 # | The logfile format is as follows. Each line consists of:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
133 # |
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
134 # | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
135 # |
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
136 # | host: Either the DNS name or the IP number of the remote client
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
137 # | rfc931: Any information returned by identd for this person,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
138 # | - otherwise.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
139 # | authuser: If user sent a userid for authentication, the user name,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
140 # | - otherwise.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
141 # | DD: Day
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
142 # | Mon: Month (calendar name)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
143 # | YYYY: Year
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
144 # | hh: hour (24-hour format, the machine's timezone)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
145 # | mm: minutes
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
146 # | ss: seconds
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
147 # | request: The first line of the HTTP request as sent by the client.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
148 # | ddd: the status code returned by the server, - if not available.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
149 # | bbbb: the total number of bytes sent,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
150 # | *not including the HTTP/1.0 header*, - if not available
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
151 # |
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
152 # | You can determine the name of the file accessed through request.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
153 #
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
154 # (Actually, the latter is only true if you know the server configuration
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
155 # at the time the request was made!)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
156
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
157
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
158 __version__ = "0.2"
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
159
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
160 __all__ = ["HTTPServer", "BaseHTTPRequestHandler"]
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
161
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
162 import sys
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
163 import time
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
164 import socket # For gethostbyaddr()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
165 import mimetools
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
166 import SocketServer
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
167
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
168 # Default error message
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
169 DEFAULT_ERROR_MESSAGE = """\
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
170 <head>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
171 <title>Error response</title>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
172 </head>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
173 <body>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
174 <h1>Error response</h1>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
175 <p>Error code %(code)d.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
176 <p>Message: %(message)s.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
177 <p>Error code explanation: %(code)s = %(explain)s.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
178 </body>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
179 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
180
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
181
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
182 class HTTPServer(SocketServer.TCPServer):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
183
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
184 allow_reuse_address = 1 # Seems to make sense in testing environment
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
185
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
186 def server_bind(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
187 """Override server_bind to store the server name."""
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
188 SocketServer.TCPServer.server_bind(self)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
189 host, port = self.socket.getsockname()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
190 self.server_name = socket.getfqdn(host)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
191 self.server_port = port
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
192
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
193
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
194 class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
195
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
196 """HTTP request handler base class.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
197
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
198 The following explanation of HTTP serves to guide you through the
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
199 code as well as to expose any misunderstandings I may have about
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
200 HTTP (so you don't need to read the code to figure out I'm wrong
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
201 :-).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
202
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
203 HTTP (HyperText Transfer Protocol) is an extensible protocol on
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
204 top of a reliable stream transport (e.g. TCP/IP). The protocol
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
205 recognizes three parts to a request:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
206
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
207 1. One line identifying the request type and path
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
208 2. An optional set of RFC-822-style headers
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
209 3. An optional data part
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
210
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
211 The headers and data are separated by a blank line.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
212
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
213 The first line of the request has the form
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
214
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
215 <command> <path> <version>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
216
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
217 where <command> is a (case-sensitive) keyword such as GET or POST,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
218 <path> is a string containing path information for the request,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
219 and <version> should be the string "HTTP/1.0". <path> is encoded
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
220 using the URL encoding scheme (using %xx to signify the ASCII
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
221 character with hex code xx).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
222
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
223 The protocol is vague about whether lines are separated by LF
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
224 characters or by CRLF pairs -- for compatibility with the widest
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
225 range of clients, both should be accepted. Similarly, whitespace
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
226 in the request line should be treated sensibly (allowing multiple
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
227 spaces between components and allowing trailing whitespace).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
228
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
229 Similarly, for output, lines ought to be separated by CRLF pairs
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
230 but most clients grok LF characters just fine.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
231
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
232 If the first line of the request has the form
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
233
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
234 <command> <path>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
235
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
236 (i.e. <version> is left out) then this is assumed to be an HTTP
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
237 0.9 request; this form has no optional headers and data part and
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
238 the reply consists of just the data.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
239
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
240 The reply form of the HTTP 1.0 protocol again has three parts:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
241
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
242 1. One line giving the response code
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
243 2. An optional set of RFC-822-style headers
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
244 3. The data
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
245
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
246 Again, the headers and data are separated by a blank line.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
247
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
248 The response code line has the form
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
249
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
250 <version> <responsecode> <responsestring>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
251
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
252 where <version> is the protocol version (always "HTTP/1.0"),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
253 <responsecode> is a 3-digit response code indicating success or
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
254 failure of the request, and <responsestring> is an optional
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
255 human-readable string explaining what the response code means.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
256
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
257 This server parses the request and the headers, and then calls a
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
258 function specific to the request type (<command>). Specifically,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
259 a request SPAM will be handled by a method do_SPAM(). If no
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
260 such method exists the server sends an error response to the
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
261 client. If it exists, it is called with no arguments:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
262
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
263 do_SPAM()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
264
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
265 Note that the request name is case sensitive (i.e. SPAM and spam
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
266 are different requests).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
267
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
268 The various request details are stored in instance variables:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
269
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
270 - client_address is the client IP address in the form (host,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
271 port);
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
272
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
273 - command, path and version are the broken-down request line;
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
274
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
275 - headers is an instance of mimetools.Message (or a derived
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
276 class) containing the header information;
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
277
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
278 - rfile is a file object open for reading positioned at the
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
279 start of the optional input data part;
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
280
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
281 - wfile is a file object open for writing.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
282
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
283 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
284
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
285 The first thing to be written must be the response line. Then
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
286 follow 0 or more header lines, then a blank line, and then the
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
287 actual data (if any). The meaning of the header lines depends on
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
288 the command executed by the server; in most cases, when data is
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
289 returned, there should be at least one header line of the form
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
290
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
291 Content-type: <type>/<subtype>
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
292
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
293 where <type> and <subtype> should be registered MIME types,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
294 e.g. "text/html" or "text/plain".
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
295
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
296 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
297
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
298 # The Python system version, truncated to its first component.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
299 sys_version = "Python/" + sys.version.split()[0]
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
300
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
301 # The server software version. You may want to override this.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
302 # The format is multiple whitespace-separated strings,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
303 # where each string is of the form name[/version].
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
304 server_version = "BaseHTTP/" + __version__
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
305
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
306 def parse_request(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
307 """Parse a request (internal).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
308
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
309 The request should be stored in self.raw_request; the results
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
310 are in self.command, self.path, self.request_version and
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
311 self.headers.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
312
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
313 Return value is 1 for success, 0 for failure; on failure, an
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
314 error is sent back.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
315
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
316 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
317 self.request_version = version = "HTTP/0.9" # Default
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
318 requestline = self.raw_requestline
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
319 if requestline[-2:] == '\r\n':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
320 requestline = requestline[:-2]
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
321 elif requestline[-1:] == '\n':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
322 requestline = requestline[:-1]
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
323 self.requestline = requestline
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
324 words = requestline.split()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
325 if len(words) == 3:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
326 [command, path, version] = words
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
327 if version[:5] != 'HTTP/':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
328 self.send_error(400, "Bad request version (%s)" % `version`)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
329 return 0
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
330 elif len(words) == 2:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
331 [command, path] = words
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
332 if command != 'GET':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
333 self.send_error(400,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
334 "Bad HTTP/0.9 request type (%s)" % `command`)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
335 return 0
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
336 else:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
337 self.send_error(400, "Bad request syntax (%s)" % `requestline`)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
338 return 0
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
339 self.command, self.path, self.request_version = command, path, version
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
340 self.headers = self.MessageClass(self.rfile, 0)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
341 return 1
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
342
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
343 def handle(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
344 """Handle a single HTTP request.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
345
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
346 You normally don't need to override this method; see the class
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
347 __doc__ string for information on how to handle specific HTTP
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
348 commands such as GET and POST.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
349
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
350 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
351
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
352 self.raw_requestline = self.rfile.readline()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
353 if not self.parse_request(): # An error code has been sent, just exit
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
354 return
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
355 mname = 'do_' + self.command
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
356 if not hasattr(self, mname):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
357 self.send_error(501, "Unsupported method (%s)" % `self.command`)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
358 return
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
359 method = getattr(self, mname)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
360 method()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
361
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
362 def send_error(self, code, message=None):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
363 """Send and log an error reply.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
364
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
365 Arguments are the error code, and a detailed message.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
366 The detailed message defaults to the short entry matching the
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
367 response code.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
368
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
369 This sends an error response (so it must be called before any
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
370 output has been generated), logs the error, and finally sends
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
371 a piece of HTML explaining the error to the user.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
372
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
373 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
374
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
375 try:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
376 short, long = self.responses[code]
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
377 except KeyError:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
378 short, long = '???', '???'
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
379 if not message:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
380 message = short
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
381 explain = long
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
382 self.log_error("code %d, message %s", code, message)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
383 self.send_response(code, message)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
384 self.send_header("Content-Type", "text/html")
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
385 self.end_headers()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
386 self.wfile.write(self.error_message_format %
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
387 {'code': code,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
388 'message': message,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
389 'explain': explain})
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
390
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
391 error_message_format = DEFAULT_ERROR_MESSAGE
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
392
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
393 def send_response(self, code, message=None):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
394 """Send the response header and log the response code.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
395
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
396 Also send two standard headers with the server software
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
397 version and the current date.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
398
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
399 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
400 self.log_request(code)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
401 if message is None:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
402 if self.responses.has_key(code):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
403 message = self.responses[code][0]
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
404 else:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
405 message = ''
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
406 if self.request_version != 'HTTP/0.9':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
407 self.wfile.write("%s %s %s\r\n" %
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
408 (self.protocol_version, str(code), message))
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
409 self.send_header('Server', self.version_string())
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
410 self.send_header('Date', self.date_time_string())
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
411
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
412 def send_header(self, keyword, value):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
413 """Send a MIME header."""
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
414 if self.request_version != 'HTTP/0.9':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
415 self.wfile.write("%s: %s\r\n" % (keyword, value))
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
416
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
417 def end_headers(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
418 """Send the blank line ending the MIME headers."""
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
419 if self.request_version != 'HTTP/0.9':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
420 self.wfile.write("\r\n")
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
421
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
422 def log_request(self, code='-', size='-'):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
423 """Log an accepted request.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
424
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
425 This is called by send_reponse().
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
426
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
427 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
428
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
429 self.log_message('"%s" %s %s',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
430 self.requestline, str(code), str(size))
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
431
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
432 def log_error(self, *args):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
433 """Log an error.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
434
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
435 This is called when a request cannot be fulfilled. By
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
436 default it passes the message on to log_message().
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
437
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
438 Arguments are the same as for log_message().
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
439
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
440 XXX This should go to the separate error log.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
441
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
442 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
443
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
444 apply(self.log_message, args)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
445
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
446 def log_message(self, format, *args):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
447 """Log an arbitrary message.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
448
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
449 This is used by all other logging functions. Override
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
450 it if you have specific logging wishes.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
451
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
452 The first argument, FORMAT, is a format string for the
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
453 message to be logged. If the format string contains
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
454 any % escapes requiring parameters, they should be
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
455 specified as subsequent arguments (it's just like
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
456 printf!).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
457
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
458 The client host and current date/time are prefixed to
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
459 every message.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
460
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
461 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
462
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
463 sys.stderr.write("%s - - [%s] %s\n" %
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
464 (self.address_string(),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
465 self.log_date_time_string(),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
466 format%args))
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
467
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
468 def version_string(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
469 """Return the server software version string."""
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
470 return self.server_version + ' ' + self.sys_version
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
471
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
472 def date_time_string(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
473 """Return the current date and time formatted for a message header."""
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
474 now = time.time()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
475 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
476 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
477 self.weekdayname[wd],
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
478 day, self.monthname[month], year,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
479 hh, mm, ss)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
480 return s
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
481
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
482 def log_date_time_string(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
483 """Return the current time formatted for logging."""
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
484 now = time.time()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
485 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
486 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
487 day, self.monthname[month], year, hh, mm, ss)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
488 return s
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
489
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
490 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
491
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
492 monthname = [None,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
493 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
494 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
495
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
496 def address_string(self):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
497 """Return the client address formatted for logging.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
498
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
499 This version looks up the full hostname using gethostbyaddr(),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
500 and tries to find a name that contains at least one dot.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
501
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
502 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
503
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
504 host, port = self.client_address
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
505 return socket.getfqdn(host)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
506
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
507 # Essentially static class variables
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
508
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
509 # The version of the HTTP protocol we support.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
510 # Don't override unless you know what you're doing (hint: incoming
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
511 # requests are required to have exactly this version string).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
512 protocol_version = "HTTP/1.0"
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
513
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
514 # The Message-like class used to parse headers
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
515 MessageClass = mimetools.Message
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
516
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
517 # Table mapping response codes to messages; entries have the
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
518 # form {code: (shortmessage, longmessage)}.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
519 # See http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
520 responses = {
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
521 200: ('OK', 'Request fulfilled, document follows'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
522 201: ('Created', 'Document created, URL follows'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
523 202: ('Accepted',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
524 'Request accepted, processing continues off-line'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
525 203: ('Partial information', 'Request fulfilled from cache'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
526 204: ('No response', 'Request fulfilled, nothing follows'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
527
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
528 301: ('Moved', 'Object moved permanently -- see URI list'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
529 302: ('Found', 'Object moved temporarily -- see URI list'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
530 303: ('Method', 'Object moved -- see Method and URL list'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
531 304: ('Not modified',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
532 'Document has not changed singe given time'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
533
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
534 400: ('Bad request',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
535 'Bad request syntax or unsupported method'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
536 401: ('Unauthorized',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
537 'No permission -- see authorization schemes'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
538 402: ('Payment required',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
539 'No payment -- see charging schemes'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
540 403: ('Forbidden',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
541 'Request forbidden -- authorization will not help'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
542 404: ('Not found', 'Nothing matches the given URI'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
543
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
544 500: ('Internal error', 'Server got itself in trouble'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
545 501: ('Not implemented',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
546 'Server does not support this operation'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
547 502: ('Service temporarily overloaded',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
548 'The server cannot process the request due to a high load'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
549 503: ('Gateway timeout',
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
550 'The gateway server did not receive a timely response'),
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
551
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
552 }
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
553
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
554
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
555 def test(HandlerClass = BaseHTTPRequestHandler,
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
556 ServerClass = HTTPServer):
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
557 """Test the HTTP request handler class.
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
558
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
559 This runs an HTTP server on port 8000 (or the first command line
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
560 argument).
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
561
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
562 """
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
563
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
564 if sys.argv[1:]:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
565 port = int(sys.argv[1])
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
566 else:
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
567 port = 8000
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
568 server_address = ('', port)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
569
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
570 httpd = ServerClass(server_address, HandlerClass)
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
571
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
572 sa = httpd.socket.getsockname()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
573 print "Serving HTTP on", sa[0], "port", sa[1], "..."
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
574 httpd.serve_forever()
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
575
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
576
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
577 if __name__ == '__main__':
c99cf31de3f2 Add CEDET tests.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
578 test()
105377
7f4c7f5c0eba Add arch tagline
Miles Bader <miles@gnu.org>
parents: 105267
diff changeset
579
7f4c7f5c0eba Add arch tagline
Miles Bader <miles@gnu.org>
parents: 105267
diff changeset
580 # arch-tag: 567449b3-cc90-45b6-bbe3-1e113995bdae