2 """ HTTP handler code for Breadcrumb servers. """
4 # Copyright (C) 2008 Laurens Van Houtven <lvh at laurensvh.be>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from common
import PointQueueHandler
, Renderer
25 class HTTPHandler(PointQueueHandler
, Renderer
):
26 """A handler for sending data over HTTP.
28 ``action`` and ``name`` mean the same thing as in an HTML form. This is
29 illustrated by the following example:
31 To POST something to a server for which the URL looks like this::
33 http://test.example:8080/my_cgi_app
35 ... and the element looks something like this::
37 <input name="foo" action="/my_cgi_app" method='POST' />
39 you would create a handler instance like this::
41 HTTPHandler(host = "text.example",
43 action = "/my_cgi_app",
46 We don't need to pass the method here, because POST is the default. You can
47 use any string httplib understands (``GET``, for example.).
49 This handler opens a new HTTP connection for each submission.
52 host
= "localhost", port
= 80,
53 action
= None, name
= None, method
= 'POST',
56 PointQueueHandler
.__init
__(self
,
57 burst_length
= burst_length
,
58 identifiers
= ('\x30', '\x31'))
59 Renderer
.__init
__(self
, type = rendertype
)
61 if not all([action
, name
, method
]):
62 raise RuntimeError, 'not enough parameters for HTTPHandler'
64 self
.action
, self
.element
, self
.method
= action
, name
, method
66 self
.host
, self
.port
= str(host
), int(port
)
69 for identifier
in ['\x30, \x31']:
70 self
.identifiers
.add(identifier
)
72 logging
.debug("Initialized HTTPHandler at %s:%s" % (host
, port
))
74 def send(self
, outstr
):
75 """Sends the given string across HTTP."""
76 parameters
= urllib
.urlencode({self
.element
: outstr
})
78 connection
= httplib
.HTTPConnection("%s:%d" % (self
.host
, self
.port
))
79 connection
.request(self
.method
, self
.action
, parameters
, self
.headers
)