routing.html 20 KB

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