Changes

Jump to navigation Jump to search
2,011 bytes added ,  16:23, 9 March 2023
no edit summary
This [[Module Library|XQuery Module]] contains functions for retrieving information on an HTTP request that has triggered the query. It is mainly mostly useful in the context of when building [[Web Application]]s.
The module is related to Adam Retter’s upcoming based on the [http://exquery.github.comio/expath-specs-playground/request-module-1.0-specification.html EXQuery Request Module]draft.
=Conventions=
* The module will be available if the {{Code|basex-api}} package must be included library is found in the classpath. This is always the case if you use one of the complete distributions of BaseX (zip, exe, war) of BaseX.* All functions are assigned to the {{Code|<code><nowiki>http://exquery.org/ns/request}} namespace. The module must be imported in the query prolog:<pre class="brush:xquery"/nowiki>import module namespace request = "http://exquery.org/ns/request";...</precode>* In this documentationnamespace, the namespace which is statically bound to the {{Code|request}} prefix.* If any of the functions is called outside the servlet context, <code>[[XQuery Errors#BaseX Errors|basex:http]]</code> is raised. The following example demonstrates illustrated what components a URI may consist of (the example is derived from [http://tools.ietf.org/html/rfc3986 RFC 3986]): 
<pre>
foo://example.com:8042/over/there?name=ferret&code=7 \_/ \_________/ \__/\_________/ \_________________________/ | | | | | scheme hostname port path query
</pre>
==request:method==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:method||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the Method of the HTTP request.
|}
 
==request:attribute==
 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|request:attribute|$name as xs:string|xs:string}}
|-
| '''Summary'''
|Returns the value of an attribute of the HTTP request. If the attribute does not exist, an empty sequence is returned.
|-
| '''Example'''
|
* {{Code|request:attribute("javax.servlet.error.request_uri")}} returns the original URI of a caught error.
* {{Code|request:attribute("javax.servlet.error.message")}} returns the error message of a caught error.
|}
==request:scheme==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:scheme||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the Scheme component of the URI of an HTTP request.
|-valign="top"
| '''Example'''
|For the example given in the introduction, this function would return {{Code|foo}}.
==request:hostname==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:hostname||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the Hostname component of the URI of an HTTP request.
|-valign="top"
| '''Example'''
|For the example given in the introduction, this function would return {{Code|example.com}}.
==request:port==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:port||() as xs:integer}}</pre>|-valign="top"
| '''Summary'''
|Returns the Port component of the URI of an HTTP request, or a default port if it has not been explicitly specified in the URI.
|-valign="top"
| '''Example'''
|For the example given in the introduction, this function would return {{Code|8042}}.
==request:path==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:path||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the Path component of the URI of an HTTP request.
|-valign="top"
| '''Example'''
|For the example given in the introduction, this function would return {{Code|/over/there}}.<br/>
==request:query==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:query||() as xs:string?}}</pre>|-valign="top"
| '''Summary'''
|Returns the Query component of the URI of an HTTP request. If no query has been specifiedcomponent exists, an empty sequence is returned.|-valign="top"
| '''Example'''
|For the example given in the introduction, this function would return <code>name=ferret&code=7</code>.<br/>
|}
==request:uri==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:uri||() as xs:anyURI}}</pre>|-valign="top"
| '''Summary'''
|Returns the complete URI of an HTTP request as it has been specified by the client.
|-valign="top"
| '''Example'''
|For the example given in the introduction, this method would return <code>foo://example.com:8042/over/there?name=ferret&code=7</code>.<br/>
|}
==request:context-path==
 
{{Mark|Introduced with Version 7.8:}}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:context-path||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the context of the request. For servlets in the default (root) context, this method returns an empty string.
==request:address==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:address||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the IP address of the server.
==request:remote-hostname==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:remote-hostname||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the fully qualified hostname of the client that sent the request.
==request:remote-address==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:remote-address||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the IP address of the client that sent the request.
==request:remote-port==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:remote-port||() as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Returns the TCP port of the client socket that triggered the request.
==request:parameter-names==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:parameter-names||() as xs:string*}}</pre>|-valign="top"
| '''Summary'''
|Returns the names of all query and form field parameters available from the HTTP request. With [[RESTXQ]], this function can be used to access parameters that have not been statically bound by [[RESTXQ#Query Parameters|%rest:query-param]] annotations.|-valign="top"
| '''Example'''
|For the example given in the introduction, this function would return {{Code|name}}.<br/>
|- valign="top"
| '''Errors'''
|{{Error|parameter|#Errors}} the request has invalid parameters.
|}
==request:parameter==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|request:parameter|$name as xs:string|xs:string*}}<br/pre>{{Func|request:parameter|( $name as xs:string, $default as xs:string| := ()) as xs:string*}}</pre>|-valign="top"
| '''Summary'''
|Returns the value of the named query or form field parameter in an HTTP request. If the parameter does not exist, an empty sequence or the optionally specified default value is returned instead. If both query and form field parameters with the same name exist, the form field values will be attached to the query values.|-valign="top"
| '''Example'''
|For the example given in the introduction, the function call {{Code|request:parameter('namecode')}} would return {{Code|ferret7}}.<br/>|- valign="top"| '''Errors'''|{{Error|parameter|#Errors}} the request has invalid parameters.
|}
==request:header-names==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:header-names||() as xs:string*}}</pre>|-valign="top"
| '''Summary'''
|Returns the names of all headers available from the HTTP request. If [[RESTXQ]] is used, this function may help can be used to find access headers that have not been statically bound by [[RESTXQ#HTTP Headers|%restxqrest:header-param]] annotations.
|}
==request:header==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|request:header|$name as xs:string|xs:string?}}<br/pre>{{Func|request:header|( $name as xs:string, $default as xs:string| := ()) as xs:string}}?</pre>|-valign="top"
| '''Summary'''
|Returns the value of the named header in an HTTP request. If the header does not exist, an empty sequence or the optionally specified default value is returned instead.
==request:cookie-names==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>request:cookie-names||() as xs:string*}}</pre>|-valign="top"
| '''Summary'''
|Returns the names of all cookies in the HTTP headers available from the HTTP request. If [[RESTXQ]] is used, this function may help can be used to find access cookies that have not been statically bound by [[RESTXQ#Cookies|%restxqrest:cookie-param]] annotations.
|}
==request:cookie==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|request:cookie|$name as xs:string|xs:string*}}<br/pre>{{Func|request:cookie|( $name as xs:string, $default as xs:string| := ()) as xs:string}}?</pre>|-valign="top"
| '''Summary'''
|Returns the value of the named Cookie in an HTTP request. If there is no such cookie, an empty sequence or the optionally specified default value is returned instead.
|}
 
=Attribute Functions=
 
==request:attribute-names==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>request:attribute-names() as xs:string*</pre>
|- valign="top"
| '''Summary'''
|Returns the names of all HTTP request attributes.
|}
 
==request:attribute==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>request:attribute(
$name as xs:string,
$default as item()* := ()
) as item()*</pre>
|- valign="top"
| '''Summary'''
|Returns the value of an attribute of the HTTP request. If the attribute does not exist, an empty sequence or the optionally specified default value is returned instead.
|- valign="top"
| '''Example'''
|
* {{Code|request:attribute("javax.servlet.error.request_uri")}} returns the original URI of a caught error.
* {{Code|request:attribute("javax.servlet.error.message")}} returns the error message of a caught error.
|}
 
==request:set-attribute==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>request:set-attribute(
$name as xs:string,
$value as item()*
) as empty-sequence()</pre>
|- valign="top"
| '''Summary'''
|Binds the specified {{Code|$value}} to the request attribute with the specified {{Code|$name}}.
|}
 
=Errors=
 
{| class="wikitable" width="100%"
! width="110"|Code
|Description
|- valign="top"
|{{Code|parameter}}
|Request has invalid parameters.
|}
=Changelog=
 
;Version 9.3
* Added: {{Function||request:attribute-names}}, {{Function||request:set-attribute}}
* Updated: {{Function||request:attribute}}: return type generalized, default argument added
 
;Version 7.9
* Updated: The returned values of {{Function||request:parameter-names}}, {{Function||request:parameter}} now also include form field parameters.
;Version 7.8
 * Added: [[#request:context-path{{Function||request:context-path]]}}
;Version 7.7
 * Added: [[#request:attribute{{Function||request:attribute]]}}
This module was introduced with Version 7.5.
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu