cgi.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>GI</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="cgi.html">GI</a>
  132. </div>
  133. <iframe class="version desktop-only" src="../version.html"></iframe>
  134. </div>
  135. <div class="content">
  136. <h1>Using CGI Programs</h1>
  137. <p>The Common Gateway Interface (CGI) is a standard for interfacing external applications with web
  138. servers. CGI was originally developed as part of the NCSA HTTP server and is an old standard for interfacing
  139. external applications with HTTP servers. It still enjoys considerable use.</p> <p>CGI was created to allow
  140. dynamic data to be generated in response to HTTP requests and return the results to the clients's browser. Plain
  141. HTML documents are typically static, while a CGI program allows the response data to be dynamically created.</p>
  142. <p>CGI scripts are written in any language that can read from the standard-input, write to the
  143. standard-output, and access environment variables. This means that virtually any programming language can be
  144. used, including C, Perl, or even Unix shell scripting.</p>
  145. <p>However, since CGI was first developed, several better means of creating dynamic web pages have been
  146. created that are faster and more efficient. Read more about such replacements in
  147. <a href="goactions.html">Using GoActions</a>.</p>
  148. <p>Embedthis GoAhead supports CGI so that existing CGI applications can be fully supported. GoAhead has a
  149. high-performance and fully featured CGI Handler that alleviates many of the pains with configuring CGI
  150. setup.</p>
  151. <a name="configuringCgi"></a>
  152. <h2 >Configuring CGI Programs</h2>
  153. <p>Requests for CGI programs are identified by a unique URI prefix specified at build time. This is
  154. typically "cgi-bin". The CGI programs and scripts are stored in special CGI directories outside the
  155. document root.</p>
  156. <p>When a URI is requested by a browser that includes the "<em>/cgi-bin/</em>" prefix, the
  157. script name immediately after "/cgi-bin/" will be run. For example:</p>
  158. <pre class="ui code segment">
  159. http://www.embedthis.com/cgi-bin/cgitest
  160. </pre>
  161. <a name="invoking"></a>
  162. <h2 >Invoking CGI Programs</h2>
  163. <p>When a CGI program is run, the GoAhead CGI handler communicates request information to the CGI program
  164. via Environment Variables.</p>
  165. <a name="env"></a>
  166. <h2 >CGI Environment Variables</h2>
  167. <p>CGI uses environment variables to send your program additional parameters. The following environment
  168. variables are defined:</p>
  169. <table title="args" class="ui table segment">
  170. <thead>
  171. <tr>
  172. <th>Variable</th>
  173. <th>Description</th>
  174. </tr>
  175. </thead>
  176. <tbody>
  177. <tr>
  178. <td>AUTH_TYPE</td>
  179. <td>Set to the value of the HTTP AUTHORIZATION header. Usually "basic", "digest" or "form".</td>
  180. </tr>
  181. <tr>
  182. <td>CONTENT_LENGTH</td>
  183. <td>Set to the length of any associated posted content.</td>
  184. </tr>
  185. <tr>
  186. <td>CONTENT_TYPE</td>
  187. <td>Set to the content mime type of any associated posted content.</td>
  188. </tr>
  189. <tr>
  190. <td>DOCUMENT_ROOT</td>
  191. <td>Set to the path location of web documents. Defined by the DocumentRoot directive in the
  192. GoAhead configuration file.</td>
  193. </tr>
  194. <tr>
  195. <td>GATEWAY_INTERFACE</td>
  196. <td>Set to "CGI/1.1"</td>
  197. </tr>
  198. <tr>
  199. <td>HTTP_ACCEPT</td>
  200. <td>Set to the value of the HTTP ACCEPT header. This specifies what formats are acceptable
  201. and/or preferable for the client.</td>
  202. </tr>
  203. <tr>
  204. <td>HTTP_CONNECTION</td>
  205. <td>Set to the value of the HTTP CONNECTION header. This specifies how the connection should be
  206. reused when the request completes. (Keep-alive)</td>
  207. </tr>
  208. <tr>
  209. <td>HTTP_HOST</td>
  210. <td>Set to the value of the HTTP HOST header. This specifies the name of the server to process
  211. the request. When using Named virtual hosting, requests to different servers (hosts) may be
  212. processed by a single HTTP server on a single IP address. The HTTP_HOST field permits the
  213. server to determine which virtual host should process the request.</td>
  214. </tr>
  215. <tr>
  216. <td>HTTP_USER_AGENT</td>
  217. <td>Set to the value of the HTTP USER_AGENT header.</td>
  218. </tr>
  219. <tr>
  220. <td>PATH_INFO</td>
  221. <td>The PATH_INFO variable is set to extra path information after the script name.</td>
  222. </tr>
  223. <tr>
  224. <td>PATH_TRANSLATED</td>
  225. <td>The physical on-disk path name corresponding to PATH_INFO.</td>
  226. </tr>
  227. <tr>
  228. <td>QUERY_STRING</td>
  229. <td>The QUERY_STRING variable is set to the URI string portion that follows the first "?" in
  230. the URI. The QUERY_STRING is note decoded.</td>
  231. </tr>
  232. <tr>
  233. <td>REMOTE_ADDR</td>
  234. <td>Set to the IP address of the requesting client.</td>
  235. </tr>
  236. <tr>
  237. <td>REMOTE_HOST</td>
  238. <td>Set to the IP address of the requesting client (same as REMOTE_ADDR).</td>
  239. </tr>
  240. <tr>
  241. <td>REMOTE_USER</td>
  242. <td>Set to the name of the authenticated user.</td>
  243. </tr>
  244. <tr>
  245. <td>REMOTE_METHOD</td>
  246. <td>Set to the HTTP method used by the request. Typical values are: "DELETE", "GET", "HEAD",
  247. "OPTIONS", "POST", "PUT", or "TRACE".</td>
  248. </tr>
  249. <tr>
  250. <td>REQUEST_URI</td>
  251. <td>The complete request URI after the host name portion. It always begins with a leading
  252. "/".</td>
  253. </tr>
  254. <tr>
  255. <td>SCRIPT_NAME</td>
  256. <td>The name of the CGI script being executed in a format suitable for self-referencing URL.</td>
  257. </tr>
  258. <tr>
  259. <td>SERVER_ADDR</td>
  260. <td>The IP address of the server or virtual host responding to the request.</td>
  261. </tr>
  262. <tr>
  263. <td>SERVER_HOST</td>
  264. <td>Set to server hostname without port.</td>
  265. </tr>
  266. <tr>
  267. <td>SERVER_NAME</td>
  268. <td>The server's hostname, alias or IP address as it would appear in self-referencing URLs.</td>
  269. </tr>
  270. <tr>
  271. <td>SERVER_PORT</td>
  272. <td>The HTTP port of the server or virtual host serving the request.</td>
  273. </tr>
  274. <tr>
  275. <td>SERVER_PROTOCOL</td>
  276. <td>Set to "HTTP/1.0" or "HTTP/1.1" depending on the protocol used by the client.</td>
  277. </tr>
  278. <tr>
  279. <td>SERVER_URL</td>
  280. <td>Set to server hostname with port. Suitable for use in a URL.</td>
  281. </tr>
  282. <tr>
  283. <td>SERVER_SOFTWARE</td>
  284. <td>Set to "Embedthis GoAhead/VERSION"</td>
  285. </tr>
  286. </tbody>
  287. </table>
  288. <h4>Example</h4>
  289. <p>Consider the following URI which will run the Perl interpreter to execute the "pricelists.pl"
  290. script.</p>
  291. <pre class="ui code segment">
  292. http://hostname/cgi-bin/myScript/products/pricelists.pl?id=23&amp;payment=creditCard
  293. </pre>
  294. <p>This URI will cause the following environment settings:</p>
  295. <table title="evn" class="ui table segment">
  296. <thead>
  297. <tr>
  298. <th>Variable</th>
  299. <th>Value</th>
  300. </tr>
  301. </thead>
  302. <tbody>
  303. <tr>
  304. <td>PATH_INFO</td>
  305. <td>/products/pricelists</td>
  306. </tr>
  307. <tr>
  308. <td>PATH_TRANSLATED</td>
  309. <td>/var/goahead/web/products/pricelists <br/>Where /var/goahead/web is the DocumentRoot</td>
  310. </tr>
  311. <tr>
  312. <td>QUERY_STRING</td>
  313. <td>id=23&amp;payment=credit+Card</td>
  314. </tr>
  315. <tr>
  316. <td>REQUEST_URI</td>
  317. <td>/cgi-bin/myScript/products/pricelists?id=23&amp;payment=credit+Card</td>
  318. </tr>
  319. <tr>
  320. <td>SCRIPT_NAME</td>
  321. <td>myScript</td>
  322. </tr>
  323. </tbody>
  324. </table>
  325. <p>This URI below demonstrates some rather cryptic encoding of URIs.
  326. The hex encoding %20, is the encoding for the space
  327. character. Once passed to the CGI program, the convention is for CGI variables to be delimited by
  328. "&amp;".</p>
  329. <pre class="ui code segment">
  330. http://hostname/cgi-bin/cgiProgram/extra/Path?var1=a+a&amp;var2=b%20b&amp;var3=c
  331. </pre>
  332. <p>This URI will cause the following environment settings:</p>
  333. <table title="env" class="ui table segment">
  334. <thead>
  335. <tr>
  336. <th>Variable</th>
  337. <th>Value</th>
  338. </tr>
  339. </thead>
  340. <tbody>
  341. <tr>
  342. <td>PATH_INFO</td>
  343. <td>/extra/Path</td>
  344. </tr>
  345. <tr>
  346. <td>PATH_TRANSLATED</td>
  347. <td>/var/goahead/web/extra/Path</td>
  348. </tr>
  349. <tr>
  350. <td>QUERY_STRING</td>
  351. <td>var1=a+a&amp;var2=b%20b&amp;var3=c</td>
  352. </tr>
  353. <tr>
  354. <td>REQUEST_URI</td>
  355. <td>/cgi-bin/cgiProgram/extra/Path?var1=a+a&amp;var2=b%20b&amp;var3=c</td>
  356. </tr>
  357. <tr>
  358. <td>SCRIPT_NAME</td>
  359. <td>cgiProgram</td>
  360. </tr>
  361. </tbody>
  362. </table>
  363. <h3>URI Encoding</h3>
  364. <p>When a URI is sent via HTTP, certain special characters must be escaped so the URI can be processed
  365. unambiguously by the server. To escape the special characters, the HTTP client should convert them to their
  366. %hex equivalent. Form and query variables are separated by "&amp;". For example: a=1&amp;b=2 defines two
  367. form variables "a" and "b" with their values equal to "1" and "2" respectively.</p><a id="cgiProgramming"></a>
  368. <h2 >CGI Programming</h2>
  369. <p>CGI program can return almost any possible content type back to the client's browser: plain HTML, audio,
  370. video or any other format. CGI programs can also control the user's browser and redirect it to another URI.
  371. To do this, CGI programs return pseudo-HTTP headers that are interpreted by GoAhead before passing the data
  372. on to the client.</p>
  373. <p>GoAhead understands the following CGI headers that can be output by the CGI program. They are
  374. case-insensitive.</p>
  375. <table title="headers" class="ui table segment">
  376. <thead>
  377. <tr>
  378. <th>Header</th>
  379. <th>Description</th>
  380. </tr>
  381. </thead>
  382. <tbody>
  383. <tr>
  384. <td class="nowrap">Content-type</td>
  385. <td>Nominate the content Mime Type. Typically "text/html". See the mime.types for a list of
  386. possible mime types.</td>
  387. </tr>
  388. <tr>
  389. <td>Status</td>
  390. <td>Set to a HTTP response code. Success is 200. Server error is 500.</td>
  391. </tr>
  392. <tr>
  393. <td>Location</td>
  394. <td>Set to the URI of a new document to which to redirect the client's browser.</td>
  395. </tr>
  396. <tr>
  397. <td>ANY</td>
  398. <td>Pass any other header back to the client.</td>
  399. </tr>
  400. </tbody>
  401. </table>
  402. <p>For example:</p>
  403. <pre class="ui code segment">
  404. Content-type: text/html
  405. &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Sample CGI Output&lt;/TITLE&gt;&lt;/HEAD&gt;
  406. &lt;BODY&gt;
  407. &lt;H1&gt;Hello World&lt;/H1&gt;
  408. &lt;/BODY&gt;&lt;/HTML&gt;
  409. </pre>
  410. <p>To redirect the browser to a new location:</p>
  411. <pre class="ui code segment">
  412. Location: /newUrl.html
  413. </pre>To signify an error in the server:
  414. <pre class="ui code segment">
  415. Status: 500
  416. </pre>
  417. <h2>CGI for VxWorks</h2>
  418. <p>CGI's standard implementation requires that standalone processes be executed and their outputs returned to the
  419. browser via the WebServer. In VxWorks, processes are not implemented, but rather tasks are. In addition to
  420. understanding the mechanisms used in the implementation of VxWorks CGI tasks, developers of CGI processes must be
  421. aware of the differences between processes on other operating systems and tasks on VxWorks.</p>
  422. <ul>
  423. <li>VxWorks tasks can be spawned using code already loaded in memory. On VxWorks systems with no file system,
  424. the CGI task code can be included in the operating system image and is not necessarily contained in a file.</li>
  425. <li>If the CGI code is contained in a file, a browser request for it will cause it to be loaded into memory
  426. prior to its execution. It will be unloaded and reloaded each time it is invoked, which allows the upgrading to
  427. a new version between invocations.</li>
  428. <li>The VxWorks taskSpawn API is used to spawn the CGI task.</li>
  429. <li> An entry point symbol name must be used to spawn the task. The request for the CGI process can define this
  430. entry point name in the request by including the query string keyword=value pair "cgientry=symbolname", where
  431. symbolname is a function name in the CGI code that is to be executed. If cgientry is not defined in this way, a
  432. default entry name will be searched for in the loaded code. The default name is "basename_cgientry", where
  433. basename is the name of the requested CGI process minus any file extension or path information (e.g., if the
  434. request is for "cgi-bin/cgitest.out", the default entry point symbol name will be "cgitest_cgientry"). If the
  435. entry point symbol name is not found or if the requested module cannot be loaded, the CGI request will fail.</li>
  436. <li>The priority of the spawned task will be the same priority at which WebServer is running.</li>
  437. <li>The stack size of the spawned task is 20K.</li>
  438. <li>The task name will be the same as the entry point name.</li>
  439. <li>The standard CGI environment variables are copied to the task environment. They can be retrieved/modified by
  440. the getenv/putenv APIs.</li>
  441. <li>Command line arguments (if any) are passed to the user's entry point via a (int argc, char **argv) standard
  442. convention, where argc is the number of arguments and argv is an array of strings.</li>
  443. <li>As in standard CGI processes, the VxWorks CGI task can retrieve additional POST data from the standard input
  444. device and must write any output to be returned to the client to the standard output device. These devices are
  445. actually temporary files where stdin and stdout have been redirected.</li>
  446. <li>User-defined CGI task codes should always be terminated with a return rather than an exit API. This allows
  447. environment space and redirected I/O files used by the task to be cleaned up and released back to the operating
  448. system appropriately.</li>
  449. </ul>
  450. <a id="hints"></a>
  451. <h2 >Hints and Tips</h2>
  452. <p>If you have special data or environment variables that must be passed to your CGI program, you can wrap
  453. it with a script that defines that environment before invoking your script.</p><a id="otherResources"></a>
  454. <h2 >Other Resources</h2>
  455. <p>The following URIs may be helpful in further reading about CGI:</p>
  456. <ul>
  457. <li>For an introduction to CGI: <a href=
  458. "http://en.wikipedia.org/wiki/Common_Gateway_Interface">http://en.wikipedia.org/wiki/Common_Gateway_Interface</a></li>
  459. <li>For the actual CGI specification: <a href=
  460. "http://tools.ietf.org/html/draft-robinson-www-interface-00">http://tools.ietf.org/html/draft-robinson-www-interface-00</a></li>
  461. <li>Other CGI resources: <a href="http://www.cgi-resources.com">http://www.cgi-resources.com</a></li>
  462. </ul>
  463. </div>
  464. <div class="terms ui basic center aligned segment">
  465. <p>&copy; Embedthis Software, 2003-2015. All rights reserved.</p>
  466. </div>
  467. <script src="../lib/jquery/jquery.min.js"></script>
  468. <script src="../lib/semantic-ui/semantic.min.js"></script>
  469. <script src="../scripts/sidebar.min.js"></script>
  470. </body>
  471. </html>