HTTP-requests.xhtml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Functions for accessing HTTP requests</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="HTTP-response.xhtml" title="Generating HTTP responses"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Functions for accessing HTTP requests" epub:type="subchapter" id="HTTP-requests">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Functions for accessing HTTP requests</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The following functions are useful for accessing
  23. properties of a HTTP request, in a Kawa program that is
  24. run either as a servlet or a CGI script. These functions
  25. can be used from plain Scheme, from KRL (whether
  26. in BRL-compatible mode or not), and from XQuery.
  27. </p>
  28. <p>The examples below assume the request <code class="literal">http://example.com:8080/myapp/foo/bar?val1=xyz&amp;val2=abc</code>, where <code class="literal">myapp</code> is the application context.
  29. We also assume that this is handled by a script <code class="literal">foo/+default+</code>.
  30. </p>
  31. <p>The file <code class="literal">testsuite/webtest/info/+default+</code> in the Kawa source distribution
  32. calls most of these functions.
  33. You can try it as described in <a class="link" href="Self-configuring-page-scripts.xhtml" title="Self-configuring web page scripts">Self-configuring page scripts</a>.
  34. </p>
  35. <section class="sect2" title="Request URL components" epub:type="division" id="idm139667870271056">
  36. <div class="titlepage">
  37. <div>
  38. <div>
  39. <h3 class="title">Request URL components</h3>
  40. </div>
  41. </div>
  42. </div>
  43. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870269984" class="indexterm"/> <code class="function">request-URI</code></p>
  44. <div class="blockquote">
  45. <blockquote class="blockquote">
  46. <p>Returns the URI of the request, as a value of type <code class="literal">URI</code>.
  47. This excludes the server specification,
  48. but includes the query string.
  49. (It is the combination of CGI variables <code class="literal">SCRIPT_NAME</code>,
  50. <code class="literal">PATH_INFO</code>, and <code class="literal">QUERY_STRING</code>.
  51. Using servlets terminology, it is the combination of
  52. Context Path, Servlet Path, PathInfo, and Query String.)
  53. </p>
  54. <pre class="screen">(request-URI) ⇒ "/myapp/foo/bar?val1=xyz&amp;val2=abc"
  55. </pre>
  56. </blockquote>
  57. </div>
  58. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870264688" class="indexterm"/> <code class="function">request-path</code></p>
  59. <div class="blockquote">
  60. <blockquote class="blockquote">
  61. <p>Returns the URI of the request, as a value of type <code class="literal">URI</code>.
  62. This excludes the server specification and the query string.
  63. Equivalent to <code class="literal">(path-file (request-URI))</code>.
  64. (It is the combination of CGI variables <code class="literal">SCRIPT_NAME</code>, and
  65. <code class="literal">PATH_INFO</code>.
  66. Same as the concatenation of <code class="literal">(request-context-path)</code>,
  67. <code class="literal">(request-script-path)</code>, and <code class="literal">(request-local-path)</code>.
  68. Using servlets terminology, it is the combination of
  69. Context Path, Servlet Path, and PathInfo.)
  70. </p>
  71. <pre class="screen">(request-path) ⇒ "/myapp/foo/bar"
  72. </pre>
  73. </blockquote>
  74. </div>
  75. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870258112" class="indexterm"/> <code class="function">request-uri</code></p>
  76. <div class="blockquote">
  77. <blockquote class="blockquote">
  78. <p>This function is deprecated, because of possible confusion
  79. with <code class="literal">request-URI</code>. Use <code class="literal">request-path</code> instead.
  80. </p>
  81. </blockquote>
  82. </div>
  83. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870254208" class="indexterm"/> <code class="function">request-url</code></p>
  84. <div class="blockquote">
  85. <blockquote class="blockquote">
  86. <p>Returns the complete URL of the request, except the query string.
  87. The result is a <code class="literal">java.lang.StringBuffer</code>.
  88. </p>
  89. <pre class="screen">(request-url) ⇒ "http://example.com:8080/myapp/foo/bar"
  90. </pre>
  91. </blockquote>
  92. </div>
  93. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870250352" class="indexterm"/> <code class="function">request-context-path</code></p>
  94. <div class="blockquote">
  95. <blockquote class="blockquote">
  96. <p>Returns the context path, relative to the server root.
  97. This is an initial substring of the <code class="literal">(request-path)</code>.
  98. Similar to the Context Path of a servlet request,
  99. except that it ends with a <code class="literal">"/"</code>.
  100. </p>
  101. <pre class="screen">(request-context-path) ⇒ "/myapp/"
  102. </pre>
  103. </blockquote>
  104. </div>
  105. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870246064" class="indexterm"/> <code class="function">request-script-path</code></p>
  106. <div class="blockquote">
  107. <blockquote class="blockquote">
  108. <p>Returns the path of the script, relative to the context.
  109. This is either an empty string, or a string that ends with <code class="literal">"/"</code>,
  110. but does not start with one. (The reason for this is to produce URIs
  111. that work better with operations like <code class="literal">resolve-uri</code>.)
  112. This is conceptually similar to <code class="literal">request-servlet-path</code>,
  113. though not always the same, and the <code class="literal">"/"</code> conventions differ.
  114. </p>
  115. <pre class="screen">(request-script-path) ⇒ "foo/"
  116. </pre>
  117. </blockquote>
  118. </div>
  119. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870240768" class="indexterm"/> <code class="function">request-local-path</code></p>
  120. <div class="blockquote">
  121. <blockquote class="blockquote">
  122. <p>Returns the remainder of the <code class="literal">request-path</code>,
  123. relative to the <code class="literal">request-script-path</code>.
  124. </p>
  125. <pre class="screen">(request-local-path) ⇒ "bar"
  126. </pre>
  127. </blockquote>
  128. </div>
  129. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870236608" class="indexterm"/> <code class="function">request-query-string</code></p>
  130. <div class="blockquote">
  131. <blockquote class="blockquote">
  132. <p>Returns the query string from an HTTP request. The query string is
  133. the part of the request URL after a question mark.
  134. Returns false if there was no query string.
  135. Corresponds to the CGI variable <code class="literal">QUERY_STRING</code>.
  136. </p>
  137. <pre class="screen">(request-query-string) ⇒ "val1=xyz&amp;val2=abc"
  138. </pre>
  139. </blockquote>
  140. </div>
  141. </section>
  142. <section class="sect2" title="Request parameters" epub:type="division" id="idm139667870232736">
  143. <div class="titlepage">
  144. <div>
  145. <div>
  146. <h3 class="title">Request parameters</h3>
  147. </div>
  148. </div>
  149. </div>
  150. <p>Request parameters are used for data returned from forms,
  151. and for other uses.
  152. They may be encoded in the query string or in the request body.
  153. </p>
  154. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870231120" class="indexterm"/> <code class="function">request-parameter</code> <em class="replaceable"><code>name</code></em> [<em class="replaceable"><code>default</code></em>]</p>
  155. <div class="blockquote">
  156. <blockquote class="blockquote">
  157. <p>If there is a parameter with the given name (a string),
  158. return the (first) corresponding value, as a string.
  159. Otherwise, return the <em class="replaceable"><code>default</code></em> value,
  160. or <code class="literal">#!null</code> if there is no <em class="replaceable"><code>default</code></em>.
  161. </p>
  162. <pre class="screen">(request-parameter "val1") ⇒ "xyz"
  163. (request-parameter "val9" "(missing)") ⇒ "(missing)"
  164. </pre>
  165. </blockquote>
  166. </div>
  167. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870225376" class="indexterm"/> <code class="function">request-parameters</code> <em class="replaceable"><code>name</code></em></p>
  168. <div class="blockquote">
  169. <blockquote class="blockquote">
  170. <p>If there is are one or more parameter with the given name (a string),
  171. return them all (as multiple values).
  172. Otherwise, return no values (i.e. <code class="literal">(values)</code>).
  173. </p>
  174. <pre class="screen">(request-parameters "val1") ⇒ "xyz"
  175. (request-parameters "val9") ⇒ #!void
  176. </pre>
  177. </blockquote>
  178. </div>
  179. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870221056" class="indexterm"/> <code class="function">request-parameter-map</code></p>
  180. <div class="blockquote">
  181. <blockquote class="blockquote">
  182. <p>Request a map of all the parameters.
  183. This is a map from strings to a sequence of strings.
  184. (Specifically, a <code class="literal">java.util.Map&lt;String,java.util.List&lt;String&gt;&gt;</code>.)
  185. </p>
  186. </blockquote>
  187. </div>
  188. </section>
  189. <section class="sect2" title="Request headers" epub:type="division" id="idm139667870217504">
  190. <div class="titlepage">
  191. <div>
  192. <div>
  193. <h3 class="title">Request headers</h3>
  194. </div>
  195. </div>
  196. </div>
  197. <p>The request headers are a set of (keyword, string)-pairs
  198. transmitted as part of the HTTP request, before the request body.
  199. </p>
  200. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870215904" class="indexterm"/> <code class="function">request-header</code> <em class="replaceable"><code>name</code></em></p>
  201. <div class="blockquote">
  202. <blockquote class="blockquote">
  203. <p>If there is a header with the given <em class="replaceable"><code>name</code></em> (a string),
  204. return the corresponding value string.
  205. Otherwise, return <code class="literal">#!null</code>.
  206. </p>
  207. <pre class="screen">(request-header "accept-language") ⇒ "en-us,en;q=0.5"
  208. </pre>
  209. </blockquote>
  210. </div>
  211. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870211248" class="indexterm"/> <code class="function">request-header-map</code></p>
  212. <div class="blockquote">
  213. <blockquote class="blockquote">
  214. <p>Request a map of all the headers.
  215. This is a map from strings to a sequence of strings.
  216. (Specifically, a <code class="literal">java.util.Map&lt;String,java.util.List&lt;String&gt;&gt;</code>.)
  217. </p>
  218. </blockquote>
  219. </div>
  220. </section>
  221. <section class="sect2" title="Request body" epub:type="division" id="idm139667870207696">
  222. <div class="titlepage">
  223. <div>
  224. <div>
  225. <h3 class="title">Request body</h3>
  226. </div>
  227. </div>
  228. </div>
  229. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870206624" class="indexterm"/> <code class="function">request-input-port</code></p>
  230. <div class="blockquote">
  231. <blockquote class="blockquote">
  232. <p>Return a textual input port for reading the request body,
  233. as a sequence of characters.
  234. </p>
  235. </blockquote>
  236. </div>
  237. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870203600" class="indexterm"/> <code class="function">request-input-stream</code></p>
  238. <div class="blockquote">
  239. <blockquote class="blockquote">
  240. <p>Return a binary input stream for reading the request body,
  241. as a sequence of bytes.
  242. </p>
  243. </blockquote>
  244. </div>
  245. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870200576" class="indexterm"/> <code class="function">request-body-string</code></p>
  246. <div class="blockquote">
  247. <blockquote class="blockquote">
  248. <p>Return the entire request body as a string
  249. </p>
  250. </blockquote>
  251. </div>
  252. </section>
  253. <section class="sect2" title="Request IP addresses and ports" epub:type="division" id="idm139667870197584">
  254. <div class="titlepage">
  255. <div>
  256. <div>
  257. <h3 class="title">Request IP addresses and ports</h3>
  258. </div>
  259. </div>
  260. </div>
  261. <p>Information about the interface and port on which the request was received.
  262. </p>
  263. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870196016" class="indexterm"/> <code class="function">request-local-socket-address</code></p>
  264. <div class="blockquote">
  265. <blockquote class="blockquote">
  266. <p>The local address on which the request was received.
  267. This is the combination of <code class="literal">(request-local-host)</code>
  268. and <code class="literal">(request-local-port)</code>, as an instance of
  269. <code class="literal">java.net.InetSocketAddress</code>.
  270. </p>
  271. </blockquote>
  272. </div>
  273. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870191760" class="indexterm"/> <code class="function">request-local-host</code></p>
  274. <div class="blockquote">
  275. <blockquote class="blockquote">
  276. <p>Get the IP address of the interface on which request was received,
  277. as an <code class="literal">java.net.InetAddress</code>.
  278. </p>
  279. </blockquote>
  280. </div>
  281. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870188448" class="indexterm"/> <code class="function">request-local-IP-address</code></p>
  282. <div class="blockquote">
  283. <blockquote class="blockquote">
  284. <p>Get the IP address of the interface on which request was received,
  285. a string in numeric form:
  286. </p>
  287. <pre class="screen">(request-local-host) ⇒ "127.0.0.1"
  288. </pre>
  289. </blockquote>
  290. </div>
  291. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870185184" class="indexterm"/> <code class="function">request-local-port</code></p>
  292. <div class="blockquote">
  293. <blockquote class="blockquote">
  294. <p>Get the port this request was received on.
  295. </p>
  296. <pre class="screen">(request-local-port) ⇒ 8080
  297. </pre>
  298. </blockquote>
  299. </div>
  300. <p>Information about the interface and port of the remote client that invoked the request.
  301. </p>
  302. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870181376" class="indexterm"/> <code class="function">request-remote-socket-address</code></p>
  303. <div class="blockquote">
  304. <blockquote class="blockquote">
  305. <p>The address of the remote client (usually a web browser)
  306. which invoked the request.
  307. This is the combination of <code class="literal">(request-remove-host)</code>
  308. and <code class="literal">(request-remote-port)</code>, as an instance of
  309. <code class="literal">java.net.InetSocketAddress</code>.
  310. </p>
  311. </blockquote>
  312. </div>
  313. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870177088" class="indexterm"/> <code class="function">request-remote-host</code></p>
  314. <div class="blockquote">
  315. <blockquote class="blockquote">
  316. <p>Get the IP address of the remote client which invoked the request,
  317. as an <code class="literal">java.net.InetAddress</code>.
  318. </p>
  319. </blockquote>
  320. </div>
  321. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870173776" class="indexterm"/> <code class="function">request-remote-IP-address</code></p>
  322. <div class="blockquote">
  323. <blockquote class="blockquote">
  324. <p>Get the IP address of the remote client which invoked the request,
  325. as a string in numeric form.
  326. </p>
  327. <pre class="screen">(request-remote-host) ⇒ "123.45.6.7"
  328. </pre>
  329. </blockquote>
  330. </div>
  331. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870170512" class="indexterm"/> <code class="function">request-remote-port</code></p>
  332. <div class="blockquote">
  333. <blockquote class="blockquote">
  334. <p>The port used by the remote client.
  335. </p>
  336. </blockquote>
  337. </div>
  338. </section>
  339. <section class="sect2" title="Miscellaneous request properties" epub:type="division" id="idm139667870167536">
  340. <div class="titlepage">
  341. <div>
  342. <div>
  343. <h3 class="title">Miscellaneous request properties</h3>
  344. </div>
  345. </div>
  346. </div>
  347. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870166448" class="indexterm"/> <code class="function">request-path-translated</code></p>
  348. <div class="blockquote">
  349. <blockquote class="blockquote">
  350. <p>Map the request-path to a file name (a string)
  351. in the server application directory.
  352. Corresponds to the CGI variable <code class="literal">PATH_TRANSLATED</code>.
  353. </p>
  354. </blockquote>
  355. </div>
  356. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870162976" class="indexterm"/> <code class="function">request-method</code></p>
  357. <div class="blockquote">
  358. <blockquote class="blockquote">
  359. <p>Returns the method of the HTTP request, usually <code class="literal">"GET"</code>
  360. or <code class="literal">"POST"</code>. Corresponds to the CGI variable <code class="literal">REQUEST_METHOD</code>.
  361. </p>
  362. </blockquote>
  363. </div>
  364. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870158656" class="indexterm"/> <code class="function">request-scheme</code></p>
  365. <div class="blockquote">
  366. <blockquote class="blockquote">
  367. <p>Returns the scheme (protocol) of the request.
  368. Usually <code class="literal">"http"</code>, or <code class="literal">"https"</code>.
  369. </p>
  370. </blockquote>
  371. </div>
  372. </section>
  373. </section>
  374. <footer>
  375. <div class="navfooter">
  376. <ul>
  377. <li>
  378. <b class="toc">
  379. <a href="HTTP-requests.xhtml#idm139667870271056">Request URL components</a>
  380. </b>
  381. </li>
  382. <li>
  383. <b class="toc">
  384. <a href="HTTP-requests.xhtml#idm139667870232736">Request parameters</a>
  385. </b>
  386. </li>
  387. <li>
  388. <b class="toc">
  389. <a href="HTTP-requests.xhtml#idm139667870217504">Request headers</a>
  390. </b>
  391. </li>
  392. <li>
  393. <b class="toc">
  394. <a href="HTTP-requests.xhtml#idm139667870207696">Request body</a>
  395. </b>
  396. </li>
  397. <li>
  398. <b class="toc">
  399. <a href="HTTP-requests.xhtml#idm139667870197584">Request IP addresses and ports</a>
  400. </b>
  401. </li>
  402. <li>
  403. <b class="toc">
  404. <a href="HTTP-requests.xhtml#idm139667870167536">Miscellaneous request properties</a>
  405. </b>
  406. </li>
  407. </ul>
  408. <p>
  409. Up: <a accesskey="u" href="XML-tools.xhtml">Working with XML and HTML</a></p>
  410. <p>
  411. Previous: <a accesskey="p" href="CGI-scripts.xhtml">Installing Kawa programs as CGI scripts</a></p>
  412. <p>
  413. Next: <a accesskey="n" href="HTTP-response.xhtml">Generating HTTP responses</a></p>
  414. </div>
  415. </footer>
  416. </body>
  417. </html>