routing.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Request Routing</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <meta charset="utf-8">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  8. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
  9. <meta name="description" content="GoAhead web server">
  10. <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Open+Sans:300italic,400,300,700' rel='stylesheet' type='text/css'>
  11. <link href='https://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'>
  12. <link href="../images/favicon.ico" rel="shortcut icon">
  13. <link href="../lib/semantic-ui/semantic.min.css" rel="stylesheet" type="text/css">
  14. <link href="../css/all.min.css" rel="stylesheet" type="text/css">
  15. <link href="../css/api.min.css" rel="stylesheet" type="text/css">
  16. </head>
  17. <body class="show-sidebar">
  18. <div class="sidebar">
  19. <div class="ui large left vertical inverted labeled menu">
  20. <div class="item">
  21. <a href="../" class="logo">GoAhead Docs</a>
  22. </div>
  23. <div class="item">
  24. <a href="../">
  25. <b>General</b>
  26. </a>
  27. <div class="menu">
  28. <a class="item" href="../">GoAhead Overview</a>
  29. <a class="item" href="../users/features.html">GoAhead Features</a>
  30. <a class="item" href="https://embedthis.com/goahead/download.html">Download</a>
  31. <a class="item" href="../licensing/">Licensing</a>
  32. </div>
  33. </div>
  34. <div class="item">
  35. <a href="../start/">
  36. <b>Getting Started</b>
  37. </a>
  38. <div class="menu">
  39. <a class="item" href="../start/quick.html">Quick Start</a>
  40. <a class="item" href="../start/installing.html">Installing GoAhead</a>
  41. <a class="item" href="../start/running.html">Running GoAhead</a>
  42. <a class="item" href="../start/releaseNotes.html">Release Notes</a>
  43. <a class="item" href="../start/faq.html">GoAhead FAQ</a>
  44. <a class="item" href="../start/source.html">Building from Source</a>
  45. </div>
  46. </div>
  47. <div class="item">
  48. <a href="../users/"><b>User's Guide</b></a>
  49. <div class="menu">
  50. <a class="item" href="../users/ports.html">Ports and Binding</a>
  51. <a class="item" href="../users/routing.html">Routing Requests</a>
  52. <a class="item" href="../users/handlers.html">Request Handlers</a>
  53. <a class="item" href="../users/js.html">Embedded Javascript</a>
  54. <a class="item" href="../users/jst.html">Javascript Templates</a>
  55. <a class="item" href="../users/goactions.html">GoActions</a>
  56. <a class="item" href="../users/cgi.html">CGI Programs</a>
  57. <a class="item" href="../users/authentication.html">User Authentication</a>
  58. <a class="item" href="../users/logFiles.html">Log Files</a>
  59. <a class="item" href="../users/ssl.html">Secure Sockets (SSL)</a>
  60. <a class="item" href="../users/security.html">Security Considerations</a>
  61. <a class="item" href="../users/man.html">Man Pages</a>
  62. </div>
  63. </div>
  64. <div class="item">
  65. <a href="../developers/">Developer's Guide</a>
  66. <div class="menu">
  67. <a class="item" href="../developers/embedding.html">Embedding GoAhead</a>
  68. <a class="item" href="../developers/handlers.html">Creating GoAhead Handlers</a>
  69. <a class="item" href="../developers/authstore.html">Creating Password Verifiers</a>
  70. <a class="item" href="../developers/migrating.html">Migrating to GoAhead 3/4</a>
  71. <a class="item" href="../developers/rom.html">Serving Pages from ROM</a>
  72. </div>
  73. </div>
  74. <div class="item">
  75. <a href="../ref/">Reference Guide</a>
  76. <div class="menu">
  77. <a class="item" href="../ref/compatibility.html">Compatibility</a>
  78. <a class="item" href="../ref/native.html">API Library</a>
  79. <a class="item" href="../ref/architecture.html">GoAhead Architecture</a>
  80. <a class="item" href="../standards/http.html">HTTP References</a>
  81. </div>
  82. </div>
  83. <div class="item">
  84. <a href="../developers/project.html">Project Resources</a>
  85. <div class="menu">
  86. <a class="item" href="https://embedthis.com/blog/categories/GoAhead/">GoAhead News</a>
  87. <a class="item" href="https://embedthis.com/goahead/">GoAhead Web Site</a>
  88. <a class="item" href="https://github.com/embedthis/goahead">Source Code Repository</a>
  89. <a class="item" href="https://github.com/embedthis/goahead/issues/99">GoAhead Security Alerts</a>
  90. <a class="item" href="https://github.com/embedthis/goahead/issues">Project Issue Database</a>
  91. <a class="item" href="https://github.com/embedthis/goahead/releases">Change Log</a>
  92. <a class="item" href="https://github.com/embedthis/goahead/milestones">Roadmap</a>
  93. <a class="item" href="https://embedthis.com/developers/contributors.html">Contributors Agreement</a>
  94. </div>
  95. </div>
  96. <div class="item">
  97. <b>Links</b>
  98. <div class="menu">
  99. <a class="item" href="https://embedthis.com/">Embedthis Web Site</a>
  100. <a class="item" href="https://embedthis.com/blog/">Embedthis Blog</a>
  101. <a class="item" href="http://twitter.com/embedthat">Twitter</a>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. <div class="ui inverted masthead">
  107. <div class="ui fixed inverted menu">
  108. <div class="ui sidebar-launch button">
  109. <i class="icon list layout"></i>
  110. </div>
  111. <div class="right menu">
  112. <a class="item" href="https://embedthis.com/">Embedthis</a>
  113. <a class="item" href="https://embedthis.com/goahead/">GoAhead Site</a>
  114. <span class="desktop-only">
  115. <a class="item" href="https://embedthis.com/blog/categories/GoAhead/">GoAhead News</a>
  116. <a class="item" href="https://github.com/embedthis/goahead">Repository</a>
  117. <a class="item" href="https://embedthis.com/blog/">Blog</a>
  118. <a class="item" href="https://twitter.com/embedthat">Twitter</a>
  119. </span>
  120. </div>
  121. </div>
  122. <div class="ui breadcrumb">
  123. <a class="section" href="../">Home</a>
  124. <div class="divider">/</div>
  125. <a class="section" href="../users/">
  126. User's Guide
  127. </a>
  128. <div class="divider">/</div>
  129. <a class="active section" href="routing.html">Request Routing</a>
  130. </div>
  131. <iframe class="version desktop-only" src="../version.html"></iframe>
  132. </div>
  133. <div class="content">
  134. <h1>Request Routing</h1>
  135. <a id="overview"></a>
  136. <h2>Overview</h2>
  137. <p>GoAhead includes a powerful request routing engine that manages how client HTTP requests are processed.
  138. The router is configured with a set of routes from a configuration file called <i>route.txt</i>. This
  139. file is loaded when GoAhead starts. When a request is received, the router tests various routes and selects
  140. the best route to handle the request. In the process, routes may redirect or rewrite the request
  141. as required.</p>
  142. <img src="../images/routing.jpg" alt="routing" class="centered">
  143. <p>A GoAhead configuration will typically have many routes. The configured routes
  144. are tested in-order by matching the route URI pattern against the request URL.
  145. A route may require that further preconditions be met before it is suitable to process the
  146. request. If the required conditions are not met, the next route in the configuration will be tested. There
  147. is always a catch-all route that will process the request if all prior routes fail to qualify.</p>
  148. <a id="configuration"></a>
  149. <h2>Route Configuration</h2>
  150. <p>Routes are defined in the <i>route.txt</i> configuration file via a series of route directives. For
  151. example:</p>
  152. <pre class="ui code segment">
  153. route uri=/cgi-bin dir=cgi-bin handler=cgi
  154. route uri=/action handler=action
  155. route uri=/ extensions=jst,asp handler=jst
  156. route uri=/ methods=OPTIONS|TRACE handler=options
  157. </pre>
  158. <p>Each line defines a new route. While route directives may have many optional parameters, every route will
  159. have a <i>uri</i> keyword that defines a URI prefix that qualifying requests must begin with.
  160. If a client request has a URI that does not being with the route URI prefix, then the route will
  161. be skipped.</p>
  162. <h3>Route Ordering</h3>
  163. <p>When multiple routes are defined in the configuration file, the router will test each route
  164. in the order in which they are declared in the configuration file. So ordering is very important.
  165. For routes with similar leading characters, define the route with the longer URI first in the
  166. route.txt file.</p>
  167. <a id="keywords"></a>
  168. <h2>Route Keywords</h2>
  169. <p>A route directive is comprised of a collection of keyword=value pairs. All routes must have a <i>uri</i>
  170. keyword, other keywords are optional. Here are the supported route keywords:</p>
  171. <a name="abilities"></a>
  172. <h3>abilities</h3>
  173. <p>The <i>abilities</i> keyword specifies the required user abilities to access resources described by
  174. this route. Multiple abilities are separated using comma. If multiple abilities are specified, all the
  175. abilities are required. For example: </p>
  176. <pre class="ui code segment">route uri=/auth/basic/ auth=basic <b>abilities=create,edit,view</b></pre>
  177. <p>To activate testing the user's abilities, an <i>auth</i> keyword must be specified to enable
  178. user authentication and authorization.
  179. See <a href="authentication.html">User Authentication</a> for more information.</p>
  180. <a name="auth"></a>
  181. <h3>auth</h3>
  182. <p>The <i>auth</i> keywords specifies the authentication scheme to use. Authentication is the
  183. process of asking for the users's name and password and then validating those credentials and
  184. determining the user's authorized abilities. Supported authentication schemes are:
  185. <i>basic</i> for Basic authentication, <i>digest</i> for Digest authentication and
  186. <i>form</i> for web-form based authentication. </p>
  187. <p>If the user's credentials cannot be authenticated, the user is redirected to a new URI specified
  188. by the <i>redirect</i> keyword. When authentication fails, GoAhead responds with a HTTP 401 status code
  189. which means unauthorized access. A redirect keyword with a 401 status code will specify the
  190. redirection target when authentication fails. For example:</p>
  191. <pre class="ui code segment">route uri=/ <b>auth=form</b> handler=continue redirect=401@/pub/login.html</pre>
  192. <p>The required abilities are specified by the <a href="#abilities">abilities</a> keyword.
  193. See <a href="authentication.html">User Authentication</a> for more information.</p>
  194. <h3>dir</h3>
  195. <p>The <i>dir</i> keyword defines the filesystem directory containing documents for this route. This overrides
  196. the default documents directory. If the client is requesting a physical document, the request URI path is
  197. appended to this directory to locate the file to serve.</p>
  198. <h3>extensions</h3>
  199. <p>The <i>extensions</i> keyword specifies the set of valid filename extensions for documents served
  200. by this route. For example:</p>
  201. <pre class="ui code segment">route uri=/ <b>extension=cgi|fcgi|mycgi</b> handler=cgi</pre>
  202. <p>This will use the CGI handler for any documents with the extensions: cgi, fcgi or mycgi.</p>
  203. <h3>handler</h3>
  204. <p>The <i>handler</i> keyword specifies the GoAhead handler that will be responsible for generating
  205. the response. If unspecified, it defaults to the <i>file</i> handler. Valid handler names include:
  206. action, cgi, file, jst, options and redirect.</p>
  207. <h3>methods</h3>
  208. <p>A route can define the a set of acceptable HTTP methods. Multiple methods should be separated by
  209. <i>|</i>. For example: </p>
  210. <pre class="ui code segment">route uri=/put/ <b>methods=PUT|DELETE</b></pre>
  211. <p>The standard HTTP methods are: DELETE, GET, OPTIONS, POST, PUT and TRACE. </p>
  212. <h3>protocol</h3>
  213. <p>A route can be restricted to a specific protocol such as HTTP or HTTPS via the protocol keyword.
  214. For example, this route will only apply to SSL requests:</p>
  215. <pre class="ui code segment">route uri=/ <b>protocol=https</b></pre>
  216. <h3>redirect</h3>
  217. <p>The <i>redirect</i> keyword specifies target URIs to which the client will be redirected based on
  218. the response HTTP status code. The format of the <i>redirect</i> keyword value is:
  219. <i>redirect=STATUS@URI</i>,
  220. where STATUS is a valid HTTP status code and URI is a valid target destination URI. For example:</p>
  221. <pre class="ui code segment">route uri=/old-content/ <b>redirect=404@/upgrade-message.html</b></pre>
  222. <p>Unlike other keywords, multiple redirect keywords with different status values
  223. can be present in a single route. </p>
  224. <h3>uri</h3>
  225. <p>The <i>uri</i> keyword is mandatory for all routes and defines the URI prefix for matching requests.
  226. All requests URIs that begin with the specified <i>uri</i> value will match. It is good practice to
  227. include a trailing <i>/</i> to match directories. For example:</p>
  228. <pre class="ui code segment">route <b>uri=/confidential/</b> auth=form abilities=top-secret</pre>
  229. <h3>Keyword Value Separators</h3>
  230. <p>Some keyword values may contain multiple values (abilities, extensions, methods). In these cases,
  231. values are separated using either "|" or ",". While both separators are valid, the convention is to
  232. use "," when all the specified values are required, and to use "|" is used when only one of the specified
  233. values is required. Specifically, abilities should be separated by "," and extensions and methods should be
  234. separated by "|".</p>
  235. <a id="processing"></a>
  236. <h2>Route Processing</h2>
  237. <p>To process a request, the GoAhead route engine examines each of the configured routes to determine the
  238. best matching route for a request. It does this by considering each route in the order they are defined in
  239. the configuration file. Each route is tested over a sequence of steps. If a route fails to match at a step, the
  240. route is discarded and the next route is considered.</p>
  241. <h3>Routing Steps</h3>
  242. <ol>
  243. <li><a href="#step-protocol">Protocol Matching</a> &mdash; Test if the request protocol matches.</li>
  244. <li><a href="#step-method">Method Matching</a> &mdash; Test if the request method matches.</li>
  245. <li><a href="#step-extension">Extension Matching</a> &mdash; Test if the request extension matches.</li>
  246. <li><a href="#step-uri">URI Matching</a> &mdash; Test if the request URI matches the route URI.</li>
  247. </ol>
  248. <a id="step-protocol"></a>
  249. <h3>Protocol Matching</h3>
  250. <p>Protocol matching is an optional step. Routes can be configured to match a specific protocol,
  251. either http or https. By default a route supports all protocols.</p>
  252. <a id="step-method"></a>
  253. <h3>Method Matching</h3>
  254. <p>Method matching is an optional step. Routes can be configured to only match certain HTTP methods.
  255. Method matching tests the actual request HTTP method against the supported route methods.
  256. By default a route supports all methods.</p>
  257. <a id="step-extension"></a>
  258. <h3>Extension Matching</h3>
  259. <p>Extension matching is an optional step. Routes can be configured to only match certain filename
  260. extensions. Extension matching tests the actual request filename against the supported route extensions.
  261. By default a route supports all extensions.</p>
  262. <a id="step-uri"></a>
  263. <h3>URI Matching</h3>
  264. <p>URI matching is a mandatory stop. The route URI is tested against the start of the request URI.</p>
  265. <a id="examples"></a>
  266. <h2>Route Examples</h2>
  267. <h3>Redirecting Requests</h3>
  268. <p>To redirect requests for old documents to newer versions:</p>
  269. <pre class="ui code segment">
  270. route uri=/oldfile.html redirect=/newfile.html
  271. </pre>
  272. <h3>Redirecting HTTP to SSL</h3>
  273. <p>To redirect all traffic over SSL:</p>
  274. <pre class="ui code segment">
  275. route uri=/ protocol=http redirect=https
  276. </pre>
  277. <h3>Controlling Access with Digest Authentication</h3>
  278. <p>To secure private content via Digest Authentication:</p>
  279. <pre class="ui code segment">
  280. route uri=/ auth=digest
  281. </pre>
  282. <h3>Enable the TRACE and OPTIONS methods</h3>
  283. <p>The TRACE and OPTIONS methods are disabled by default to enhance security.
  284. These methods should not be enabled for all routes. To selectively enable:</p>
  285. <pre class="ui code segment">
  286. route uri=/partition/ methods=OPTIONS|TRACE handler=options
  287. </pre>
  288. <h3>Define a New Extension</h3>
  289. <p>To ensure that requests for content with given file extensions are handled
  290. by a specific handler, you can use the <i>extensions</i> and <i>handler</i> keywords:</p>
  291. <pre class="ui code segment">
  292. route uri=/ extensions=jst,asp handler=jst
  293. </pre>
  294. <h3>Catch-All Route</h3>
  295. <p>If all prior routes file to match a request, a catch-all route can be used to respond universally.</p>
  296. <pre class="ui code segment">route uri=/</pre>
  297. <p>Note: if the handler is not specified, the <i>file</i> handler is used.</p>
  298. </div>
  299. <div class="terms ui basic center aligned segment">
  300. <p>&copy; Embedthis Software. All rights reserved.</p>
  301. </div>
  302. <script src="../lib/jquery/jquery.min.js"></script>
  303. <script src="../lib/semantic-ui/semantic.min.js"></script>
  304. <script src="../scripts/sidebar.min.js"></script>