spacepaste

  1.  
  2. class IRequest(IComponentized):
  3. """A HTTP request.
  4. Subclasses should override the process() method to determine how
  5. the request will be processed.
  6. @ivar method: The HTTP method that was used.
  7. @ivar uri: The full URI that was requested (includes arguments).
  8. @ivar path: The path only (arguments not included).
  9. @ivar args: All of the arguments, including URL and POST arguments.
  10. @type args: A mapping of strings (the argument names) to lists of values.
  11. i.e., ?foo=bar&foo=baz&quux=spam results in
  12. {'foo': ['bar', 'baz'], 'quux': ['spam']}.
  13. @ivar received_headers: All received headers.
  14. """
  15. method = Attribute("The HTTP method that was used.")
  16. uri = Attribute("The full URI that was requested (includes arguments).")
  17. path = Attribute("The path only (arguments not included).")
  18. prepath = Attribute("Path segments that have already been handled.")
  19. postpath = Attribute("Path segments still to be handled.")
  20. args = Attribute("All of the arguments, including URL and POST arguments.")
  21. received_headers = Attribute("All received headers.")
  22. deferred = Attribute("Fired once request processing is finished.")
  23. client = Attribute("The client that sent this request.")
  24. content = Attribute("File-like object containing the request body.")
  25. # Methods for received request
  26. def getHeader(key):
  27. """Get a header that was sent from the network.
  28. Return C{None} if the header is not present.
  29. """
  30. def getCookie(key):
  31. """Get a cookie that was sent from the network.
  32. """
  33. def getAllHeaders():
  34. """Return dictionary of all headers the request received."""
  35. def getRequestHostname():
  36. """Get the hostname that the user passed in to the request.
  37. This will either use the Host: header (if it is available) or the
  38. host we are listening on if the header is unavailable.
  39. """
  40. def getHost():
  41. """Get my originally requesting transport's host.
  42. Don't rely on the 'transport' attribute, since Request objects may be
  43. copied remotely. For information on this method's return value, see
  44. twisted.internet.tcp.Port.
  45. """
  46. def getClientIP():
  47. pass
  48. def getClient():
  49. pass
  50. def getUser():
  51. pass
  52. def getPassword():
  53. pass
  54. def isSecure():
  55. pass
  56. def getSession(sessionInterface = None):
  57. pass
  58. def URLPath():
  59. pass
  60. def prePathURL():
  61. pass
  62. def rememberRootURL():
  63. """
  64. Remember the currently-processed part of the URL for later
  65. recalling.
  66. """
  67. def getRootURL():
  68. """
  69. Get a previously-remembered URL.
  70. """
  71. # Methods for outgoing request
  72. def finish():
  73. """We are finished writing data."""
  74. def write(data):
  75. """
  76. Write some data as a result of an HTTP request. The first
  77. time this is called, it writes out response data.
  78. """
  79. def addCookie(k, v, expires=None, domain=None, path=None, max_age=None, comment=None, secure=None):
  80. """Set an outgoing HTTP cookie.
  81. In general, you should consider using sessions instead of cookies, see
  82. twisted.web.server.Request.getSession and the
  83. twisted.web.server.Session class for details.
  84. """
  85. def setResponseCode(code, message=None):
  86. """Set the HTTP response code.
  87. """
  88. def setHeader(k, v):
  89. """Set an outgoing HTTP header.
  90. """
  91. def redirect(url):
  92. """Utility function that does a redirect.
  93. The request should have finish() called after this.
  94. """
  95. def setLastModified(when):
  96. """Set the X{Last-Modified} time for the response to this request.
  97. If I am called more than once, I ignore attempts to set
  98. Last-Modified earlier, only replacing the Last-Modified time
  99. if it is to a later value.
  100. If I am a conditional request, I may modify my response code
  101. to L{NOT_MODIFIED} if appropriate for the time given.
  102. @param when: The last time the resource being returned was
  103. modified, in seconds since the epoch.
  104. @type when: number
  105. @return: If I am a X{If-Modified-Since} conditional request and
  106. the time given is not newer than the condition, I return
  107. L{http.CACHED<CACHED>} to indicate that you should write no
  108. body. Otherwise, I return a false value.
  109. """
  110. def setETag(etag):
  111. """Set an X{entity tag} for the outgoing response.
  112. That's \"entity tag\" as in the HTTP/1.1 X{ETag} header, \"used
  113. for comparing two or more entities from the same requested
  114. resource.\"
  115. If I am a conditional request, I may modify my response code
  116. to L{NOT_MODIFIED<twisted.protocols.http.NOT_MODIFIED>} or
  117. L{PRECONDITION_FAILED<twisted.protocols.http.PRECONDITION_FAILED>},
  118. if appropriate for the tag given.
  119. @param etag: The entity tag for the resource being returned.
  120. @type etag: string
  121. @return: If I am a X{If-None-Match} conditional request and
  122. the tag matches one in the request, I return
  123. L{CACHED<twisted.protocols.http.CACHED>} to indicate that
  124. you should write no body. Otherwise, I return a false
  125. value.
  126. """
  127. def setHost(host, port, ssl=0):
  128. """Change the host and port the request thinks it's using.
  129. This method is useful for working with reverse HTTP proxies (e.g.
  130. both Squid and Apache's mod_proxy can do this), when the address
  131. the HTTP client is using is different than the one we're listening on.
  132. For example, Apache may be listening on https://www.example.com, and then
  133. forwarding requests to http://localhost:8080, but we don't want HTML produced
  134. by Twisted to say 'http://localhost:8080', they should say 'https://www.example.com',
  135. so we do:
  136. >>> request.setHost('www.example.com', 443, ssl=1)
  137. This method is experimental.
  138. """
  139. def notifyFinish(success):
  140. """
  141. Return a deferred that fires when the request is finished.
  142. The deferred will fire with C{None} if the request finished
  143. successfully, or with the error that caused it to be unsuccessful.
  144. """
  145. class IRequest(IComponentized):
  146. """A HTTP request.
  147. Subclasses should override the process() method to determine how
  148. the request will be processed.
  149. @ivar method: The HTTP method that was used.
  150. @ivar uri: The full URI that was requested (includes arguments).
  151. @ivar path: The path only (arguments not included).
  152. @ivar args: All of the arguments, including URL and POST arguments.
  153. @type args: A mapping of strings (the argument names) to lists of values.
  154. i.e., ?foo=bar&foo=baz&quux=spam results in
  155. {'foo': ['bar', 'baz'], 'quux': ['spam']}.
  156. @ivar received_headers: All received headers.
  157. """
  158. method = Attribute("The HTTP method that was used.")
  159. uri = Attribute("The full URI that was requested (includes arguments).")
  160. path = Attribute("The path only (arguments not included).")
  161. prepath = Attribute("Path segments that have already been handled.")
  162. postpath = Attribute("Path segments still to be handled.")
  163. args = Attribute("All of the arguments, including URL and POST arguments.")
  164. received_headers = Attribute("All received headers.")
  165. deferred = Attribute("Fired once request processing is finished.")
  166. client = Attribute("The client that sent this request.")
  167. content = Attribute("File-like object containing the request body.")
  168. # Methods for received request
  169. def getHeader(key):
  170. """Get a header that was sent from the network.
  171. Return C{None} if the header is not present.
  172. """
  173. def getCookie(key):
  174. """Get a cookie that was sent from the network.
  175. """
  176. def getAllHeaders():
  177. """Return dictionary of all headers the request received."""
  178. def getRequestHostname():
  179. """Get the hostname that the user passed in to the request.
  180. This will either use the Host: header (if it is available) or the
  181. host we are listening on if the header is unavailable.
  182. """
  183. def getHost():
  184. """Get my originally requesting transport's host.
  185. Don't rely on the 'transport' attribute, since Request objects may be
  186. copied remotely. For information on this method's return value, see
  187. twisted.internet.tcp.Port.
  188. """
  189. def getClientIP():
  190. pass
  191. def getClient():
  192. pass
  193. def getUser():
  194. pass
  195. def getPassword():
  196. pass
  197. def isSecure():
  198. pass
  199. def getSession(sessionInterface = None):
  200. pass
  201. def URLPath():
  202. pass
  203. def prePathURL():
  204. pass
  205. def rememberRootURL():
  206. """
  207. Remember the currently-processed part of the URL for later
  208. recalling.
  209. """
  210. def getRootURL():
  211. """
  212. Get a previously-remembered URL.
  213. """
  214. # Methods for outgoing request
  215. def finish():
  216. """We are finished writing data."""
  217. def write(data):
  218. """
  219. Write some data as a result of an HTTP request. The first
  220. time this is called, it writes out response data.
  221. """
  222. def addCookie(k, v, expires=None, domain=None, path=None, max_age=None, comment=None, secure=None):
  223. """Set an outgoing HTTP cookie.
  224. In general, you should consider using sessions instead of cookies, see
  225. twisted.web.server.Request.getSession and the
  226. twisted.web.server.Session class for details.
  227. """
  228. def setResponseCode(code, message=None):
  229. """Set the HTTP response code.
  230. """
  231. def setHeader(k, v):
  232. """Set an outgoing HTTP header.
  233. """
  234. def redirect(url):
  235. """Utility function that does a redirect.
  236. The request should have finish() called after this.
  237. """
  238. def setLastModified(when):
  239. """Set the X{Last-Modified} time for the response to this request.
  240. If I am called more than once, I ignore attempts to set
  241. Last-Modified earlier, only replacing the Last-Modified time
  242. if it is to a later value.
  243. If I am a conditional request, I may modify my response code
  244. to L{NOT_MODIFIED} if appropriate for the time given.
  245. @param when: The last time the resource being returned was
  246. modified, in seconds since the epoch.
  247. @type when: number
  248. @return: If I am a X{If-Modified-Since} conditional request and
  249. the time given is not newer than the condition, I return
  250. L{http.CACHED<CACHED>} to indicate that you should write no
  251. body. Otherwise, I return a false value.
  252. """
  253. def setETag(etag):
  254. """Set an X{entity tag} for the outgoing response.
  255. That's \"entity tag\" as in the HTTP/1.1 X{ETag} header, \"used
  256. for comparing two or more entities from the same requested
  257. resource.\"
  258. If I am a conditional request, I may modify my response code
  259. to L{NOT_MODIFIED<twisted.protocols.http.NOT_MODIFIED>} or
  260. L{PRECONDITION_FAILED<twisted.protocols.http.PRECONDITION_FAILED>},
  261. if appropriate for the tag given.
  262. @param etag: The entity tag for the resource being returned.
  263. @type etag: string
  264. @return: If I am a X{If-None-Match} conditional request and
  265. the tag matches one in the request, I return
  266. L{CACHED<twisted.protocols.http.CACHED>} to indicate that
  267. you should write no body. Otherwise, I return a false
  268. value.
  269. """
  270. def setHost(host, port, ssl=0):
  271. """Change the host and port the request thinks it's using.
  272. This method is useful for working with reverse HTTP proxies (e.g.
  273. both Squid and Apache's mod_proxy can do this), when the address
  274. the HTTP client is using is different than the one we're listening on.
  275. For example, Apache may be listening on https://www.example.com, and then
  276. forwarding requests to http://localhost:8080, but we don't want HTML produced
  277. by Twisted to say 'http://localhost:8080', they should say 'https://www.example.com',
  278. so we do:
  279. >>> request.setHost('www.example.com', 443, ssl=1)
  280. This method is experimental.
  281. """
  282. def notifyFinish(success):
  283. """
  284. Return a deferred that fires when the request is finished.
  285. The deferred will fire with C{None} if the request finished
  286. successfully, or with the error that caused it to be unsuccessful.
  287. """
  288.