routing.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. {
  2. title: 'Request Routing',
  3. crumbs: [
  4. { "User's Guide": '../users/' },
  5. ],
  6. }
  7. <h1>Request Routing</h1>
  8. <a id="overview"></a>
  9. <h2>Overview</h2>
  10. <p>GoAhead includes a powerful request routing engine that manages how client HTTP requests are processed.
  11. The router is configured with a set of routes from a configuration file called <i>route.txt</i>. This
  12. file is loaded when GoAhead starts. When a request is received, the router tests various routes and selects
  13. the best route to handle the request. In the process, routes may redirect or rewrite the request
  14. as required.</p>
  15. <img src="../images/routing.jpg" alt="routing" class="centered" />
  16. <p>A GoAhead configuration will typically have many routes. The configured routes
  17. are tested in-order by matching the route URI pattern against the request URL.
  18. A route may require that further preconditions be met before it is suitable to process the
  19. request. If the required conditions are not met, the next route in the configuration will be tested. There
  20. is always a catch-all route that will process the request if all prior routes fail to qualify.</p>
  21. <a id="configuration"></a>
  22. <h2 >Route Configuration</h2>
  23. <p>Routes are defined in the <i>route.txt</i> configuration file via a series of route directives. For
  24. example:</p>
  25. <pre class="ui code segment">
  26. route uri=/cgi-bin dir=cgi-bin handler=cgi
  27. route uri=/action handler=action
  28. route uri=/ extensions=jst,asp handler=jst
  29. route uri=/ methods=OPTIONS|TRACE handler=options
  30. </pre>
  31. <p>Each line defines a new route. While route directives may have many optional parameters, every route will
  32. have a <i>uri</i> keyword that defines a URI prefix that qualifying requests must begin with.
  33. If a client request has a URI that does not being with the route URI prefix, then the route will
  34. be skipped.</p>
  35. <h3>Route Ordering</h3>
  36. <p>When multiple routes are defined in the configuration file, the router will test each route
  37. in the order in which they are declared in the configuration file. So ordering is very important.
  38. For routes with similar leading characters, define the route with the longer URI first in the
  39. route.txt file.</p>
  40. <a id="keywords"></a>
  41. <h2 >Route Keywords</h2>
  42. <p>A route directive is comprised of a collection of keyword=value pairs. All routes must have a <i>uri</i>
  43. keyword, other keywords are optional. Here are the supported route keywords:</p>
  44. <a name="abilities"></a>
  45. <h3>abilities</h3>
  46. <p>The <i>abilities</i> keyword specifies the required user abilities to access resources described by
  47. this route. Multiple abilities are separated using comma. If multiple abilities are specified, all the
  48. abilities are required. For example: </p>
  49. <pre class="ui code segment">route uri=/auth/basic/ auth=basic <b>abilities=create,edit,view</b></pre>
  50. <p>To activate testing the user's abilities, an <i>auth</i> keyword must be specified to enable
  51. user authentication and authorization.
  52. See <a href="authentication.html">User Authentication</a> for more information.</p>
  53. <a name="auth"></a>
  54. <h3>auth</h3>
  55. <p>The <i>auth</i> keywords specifies the authentication scheme to use. Authentication is the
  56. process of asking for the users's name and password and then validating those credentials and
  57. determining the user's authorized abilities. Supported authentication schemes are:
  58. <i>basic</i> for Basic authentication, <i>digest</i> for Digest authentication and
  59. <i>form</i> for web-form based authentication. </p>
  60. <p>If the user's credentials cannot be authenticated, the user is redirected to a new URI specified
  61. by the <i>redirect</i> keyword. When authentication fails, GoAhead responds with a HTTP 401 status code
  62. which means unauthorized access. A redirect keyword with a 401 status code will specify the
  63. redirection target when authentication fails. For example:</p>
  64. <pre class="ui code segment">route uri=/ <b>auth=form</b> handler=continue redirect=401@/pub/login.html</pre>
  65. <p>The required abilities are specified by the <a href="#abilities">abilities</a> keyword.
  66. See <a href="authentication.html">User Authentication</a> for more information.</p>
  67. <h3>dir</h3>
  68. <p>The <i>dir</i> keyword defines the filesystem directory containing documents for this route. This overrides
  69. the default documents directory. If the client is requesting a physical document, the request URI path is
  70. appended to this directory to locate the file to serve.</p>
  71. <h3>extensions</h3>
  72. <p>The <i>extensions</i> keyword specifies the set of valid filename extensions for documents served
  73. by this route. For example:</p>
  74. <pre class="ui code segment">route uri=/ <b>extension=cgi|fcgi|mycgi</b> handler=cgi</pre>
  75. <p>This will use the CGI handler for any documents with the extensions: cgi, fcgi or mycgi.</p>
  76. <h3>handler</h3>
  77. <p>The <i>handler</i> keyword specifies the GoAhead handler that will be responsible for generating
  78. the response. If unspecified, it defaults to the <i>file</i> handler. Valid handler names include:
  79. action, cgi, file, jst, options and redirect.</p>
  80. <h3>methods</h3>
  81. <p>A route can define the a set of acceptable HTTP methods. Multiple methods should be separated by
  82. <i>|</i>. For example: </p>
  83. <pre class="ui code segment">route uri=/put/ <b>methods=PUT|DELETE</b></pre>
  84. <p>The standard HTTP methods are: DELETE, GET, OPTIONS, POST, PUT and TRACE. </p>
  85. <h3>protocol</h3>
  86. <p>A route can be restricted to a specific protocol such as HTTP or HTTPS via the protocol keyword.
  87. For example, this route will only apply to SSL requests:</p>
  88. <pre class="ui code segment">route uri=/ <b>protocol=https</b></pre>
  89. <h3>redirect</h3>
  90. <p>The <i>redirect</i> keyword specifies target URIs to which the client will be redirected based on
  91. the response HTTP status code. The format of the <i>redirect</i> keyword value is:
  92. <i>redirect=STATUS@URI</i>,
  93. where STATUS is a valid HTTP status code and URI is a valid target destination URI. For example:</p>
  94. <pre class="ui code segment">route uri=/old-content/ <b>redirect=404@/upgrade-message.html</b></pre>
  95. <p>Unlike other keywords, multiple redirect keywords with different status values
  96. can be present in a single route. </p>
  97. <h3>uri</h3>
  98. <p>The <i>uri</i> keyword is mandatory for all routes and defines the URI prefix for matching requests.
  99. All requests URIs that begin with the specified <i>uri</i> value will match. It is good practice to
  100. include a trailing <i>/</i> to match directories. For example:</p>
  101. <pre class="ui code segment">route <b>uri=/confidential/</b> auth=form abilities=top-secret</pre>
  102. <h3>Keyword Value Separators</h3>
  103. <p>Some keyword values may contain multiple values (abilities, extensions, methods). In these cases,
  104. values are separated using either "|" or ",". While both separators are valid, the convention is to
  105. use "," when all the specified values are required, and to use "|" is used when only one of the specified
  106. values is required. Specifically, abilities should be separated by "," and extensions and methods should be
  107. separated by "|".</p>
  108. <a id="processing"></a>
  109. <h2 >Route Processing</h2>
  110. <p>To process a request, the GoAhead route engine examines each of the configured routes to determine the
  111. best matching route for a request. It does this by considering each route in the order they are defined in
  112. the configuration file. Each route is tested over a sequence of steps. If a route fails to match at a step, the
  113. route is discarded and the next route is considered.</p>
  114. <h3>Routing Steps</h3>
  115. <ol>
  116. <li><a href="#step-protocol">Protocol Matching</a> &mdash; Test if the request protocol matches.</li>
  117. <li><a href="#step-method">Method Matching</a> &mdash; Test if the request method matches.</li>
  118. <li><a href="#step-extension">Extension Matching</a> &mdash; Test if the request extension matches.</li>
  119. <li><a href="#step-uri">URI Matching</a> &mdash; Test if the request URI matches the route URI.</li>
  120. </ol>
  121. <a id="step-protocol"></a>
  122. <h3>Protocol Matching</h3>
  123. <p>Protocol matching is an optional step. Routes can be configured to match a specific protocol,
  124. either http or https. By default a route supports all protocols.</p>
  125. <a id="step-method"></a>
  126. <h3>Method Matching</h3>
  127. <p>Method matching is an optional step. Routes can be configured to only match certain HTTP methods.
  128. Method matching tests the actual request HTTP method against the supported route methods.
  129. By default a route supports all methods.</p>
  130. <a id="step-extension"></a>
  131. <h3>Extension Matching</h3>
  132. <p>Extension matching is an optional step. Routes can be configured to only match certain filename
  133. extensions. Extension matching tests the actual request filename against the supported route extensions.
  134. By default a route supports all extensions.</p>
  135. <a id="step-uri"></a>
  136. <h3>URI Matching</h3>
  137. <p>URI matching is a mandatory stop. The route URI is tested against the start of the request URI.</p>
  138. <a id="examples"></a>
  139. <h2 >Route Examples</h2>
  140. <h3>Redirecting Requests</h3>
  141. <p>To redirect requests for old documents to newer versions:</p>
  142. <pre class="ui code segment">
  143. route uri=/oldfile.html redirect=/newfile.html
  144. </pre>
  145. <h3>Redirecting HTTP to SSL</h3>
  146. <p>To redirect all traffic over SSL:</p>
  147. <pre class="ui code segment">
  148. route uri=/ protocol=http redirect=https
  149. </pre>
  150. <h3>Controlling Access with Digest Authentication</h3>
  151. <p>To secure private content via Digest Authentication:</p>
  152. <pre class="ui code segment">
  153. route uri=/ auth=digest
  154. </pre>
  155. <h3>Enable the TRACE and OPTIONS methods</h3>
  156. <p>The TRACE and OPTIONS methods are disabled by default to enhance security.
  157. These methods should not be enabled for all routes. To selectively enable:</p>
  158. <pre class="ui code segment">
  159. route uri=/partition/ methods=OPTIONS|TRACE handler=options
  160. </pre>
  161. <h3>Define a New Extension</h3>
  162. <p>To ensure that requests for content with given file extensions are handled
  163. by a specific handler, you can use the <i>extensions</i> and <i>handler</i> keywords:</p>
  164. <pre class="ui code segment">
  165. route uri=/ extensions=jst,asp handler=jst
  166. </pre>
  167. <h3>Catch-All Route</h3>
  168. <p>If all prior routes file to match a request, a catch-all route can be used to respond universally.</p>
  169. <pre class="ui code segment">route uri=/</pre>
  170. <p>Note: if the handler is not specified, the <i>file</i> handler is used.</p>