handlers.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Creating Handlers</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="../developers/">
  128. Developer's Guide
  129. </a>
  130. <div class="divider">/</div>
  131. <a class="active section" href="handlers.html">Creating Handlers</a>
  132. </div>
  133. <iframe class="version desktop-only" src="../version.html"></iframe>
  134. </div>
  135. <div class="content">
  136. <h1>Creating GoAhead Handlers</h1>
  137. <p>GoAhead responds to client requests by routing the request to a request handler. The
  138. request handler is responsible for generating the response content or redirecting to another
  139. more suitable handler.</p>
  140. <p>GoAhead provides a suite of handlers for standard content types and web frameworks. But you can also create your own custom handler to process Http requests and perform any processing you desire. Once created handlers are configured via routes in the route table. See <a href="../users/routing.html">Request Routing</a> for more details about Routing.</p>
  141. <a id="processing"></a>
  142. <h2>Request Processing</h2>
  143. <p>When a request is received from the client, GoAhead parses the HTTP request headers and then determines the best GoAhead <a href="../users/routing.html">route</a> for the request. A route contains the full details for how to process a request including the required handler and required authentication. GoAhead matches each route in the route table in-order and selects the first matching route. The route specifies the desired handler for requests matching that route.</p>
  144. <h3>Matching a Handler</h3>
  145. <p>The last step of selecting a route is calling the candidate handler's optional match() callback. If the match callback returns true, the handler will be selected. If it returns false, the handler and route will be skipped and the route selection process continues.</p>
  146. <h3>Running a Handler</h3>
  147. <p>Once the route has been selected, GoAhead invokes the handler service callback to process the request and generate a response. At this point, request body data will be received and buffered. The handler may choose to not handler the request by returning a zero status code. In that case, the router continues matching routes to find a more suitable route and handler combination.</p>
  148. <a id="responses"></a>
  149. <h2>Generating Responses</h2>
  150. <p>An HTTP response consists of a status code, a set of HTTP headers and optionally a response body. If a
  151. status is not set, the successful status of 200 will be used. If not custom headers are defined, then a
  152. minimal standard set will be generated.</p>
  153. <h3>Setting Status and Headers</h3>
  154. <p>The response status may be set via:
  155. <a href="../ref/api/goahead.html#group___webs_1gaef03eccfb6f0d42b34f1a7a90bac62b6">websSetStatus</a>.
  156. The response headers may be set via:
  157. <a href="../ref/api/goahead.html#group___webs_1ga506c041a3eb2dfeaab1e9d1f322eea0b">websWriteHeaders</a>.
  158. For example:</p>
  159. <pre class="ui code segment">
  160. websSetStatus(wp, 200);
  161. websWriteHeaders(wp, contentLength, 0);
  162. websWriteHeader(wp, "X-MyCustomHeader", "1234");
  163. websWriteEndHeaders(wp);
  164. websWriteBlock(wp, buf, len);
  165. websDone(wp);
  166. </pre>
  167. <p>The websWriteBlock function will buffer written data that will be written to the client in the
  168. background.</p>
  169. <a id="errors"></a>
  170. <h3>Generating an Error Response</h3>
  171. <p>If the request has an error, the status and a response message may be set in one step via:
  172. <a href="../ref/api/goahead.html#group___webs_1ga3a9158494088fc25249543e69481e00e">websError</a>.
  173. When websError is called to indicate a request error, the supplied response text is used instead of
  174. any partially generated response body and the the connection field <em>conn-&gt;error</em> is set. Once
  175. set, pipeline processing is abbreviated and handler callbacks will not be called anymore. Consequently, if
  176. you need to continue handler processing, but want to set a non-zero status return code, do <i>not</i>
  177. use websError. Rather, use websSetStatus.</p>
  178. <pre class="ui code segment">
  179. websError(conn, 404, "Can't find %s", path);
  180. </pre>
  181. <h4>Aborting Requests</h4>
  182. <p>The status argument to websError can also accept flags to control how the socket connection is
  183. managed. If WEBS_CLOSE is supplied, the connection will be closed when the request is completed.
  184. Normally the connection is kept-open for subsequent requests on the same socket.</p>
  185. <pre class="ui code segment">
  186. websError(conn, 404 | WEBS_CLOSE, "Protocol error");
  187. </pre>
  188. <a id="redirecting"></a>
  189. <h3>Redirecting</h3>
  190. <p>Sometimes a handler will want to generate a response that will redirect the client to a new URI.
  191. Use the
  192. <a href="../ref/api/goahead.html#group___webs_1ga2a3e594ef28c12f3be0d7b54461af36e">websRedirect</a> call
  193. to redirect the client. For example:
  194. <pre class="ui code segment">websRedirect(wp, WEBS_CODE_MOVED_PERMANENTLY, uri);</pre>
  195. <h3>Generating Response Body</h3>
  196. <p>The simplest way to generate a response is to use
  197. <a href="../ref/api/goahead.html#group___webs_1ga8d5489a7fa2a2126bc0b6c703da4b964">websWrite</a>.
  198. The websWrite routine will automatically flush data as required. This routine may block while data drains
  199. to the client if the data cannot be buffered. See the <i>main.bit</i> configuration limit:
  200. <a href="../start/source.html#main">LimitBuffer</a> for how to control the internal buffer size.
  201. When all the data has been written, call:
  202. <a href="../ref/api/goahead.html#group___webs_1gac2af58b7a686cb33e44eb010cf755ce1">websDone</a>. This
  203. finalizes the request.</p>
  204. <pre class="ui code segment">
  205. websWrite(p, "Hello World\n");
  206. websDone(wp);
  207. </pre>
  208. <a id="defining"></a>
  209. <h2>Defining a Handler</h2>
  210. <p>To define an GoAhead handler, you need to call <a href=
  211. "../ref/api/goahead.html#group___webs_1gaac26a36fb28e1486fd54443ad59674a5">websDefineHandler</a>. For example:
  212. <pre class="ui code segment">
  213. static bool matchFoo(Webs *wp)
  214. {
  215. /* Handle request */
  216. return 1;
  217. }
  218. static bool serviceFoo(Webs *wp)
  219. {
  220. /* Do work here */
  221. return 1;
  222. }
  223. static void closeFoo() {
  224. /* Cleanup when goahead is exiting */
  225. }
  226. ...
  227. websDefineHandler("foo", matchFoo, serviceFoo, closeFoo, 0);
  228. </pre>
  229. </div>
  230. <div class="terms ui basic center aligned segment">
  231. <p>&copy; Embedthis Software, 2003-2015. All rights reserved.</p>
  232. </div>
  233. <script src="../lib/jquery/jquery.min.js"></script>
  234. <script src="../lib/semantic-ui/semantic.min.js"></script>
  235. <script src="../scripts/sidebar.min.js"></script>
  236. </body>
  237. </html>