test.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. server.c - Appweb server to demonstrate actions
  3. Copyright (c) All Rights Reserved. See copyright notice at the bottom of the file.
  4. */
  5. /******************************* Includes *****************************/
  6. #include "appweb.h"
  7. /********************************* Code *******************************/
  8. /*
  9. This method is run when the action form is called from the web page.
  10. */
  11. static void myaction(HttpStream *stream)
  12. {
  13. HttpQueue *q;
  14. q = stream->writeq;
  15. /*
  16. Set the HTTP response status
  17. */
  18. httpSetStatus(stream, 200);
  19. /*
  20. Add desired headers. "Set" will overwrite, add will create if not already defined.
  21. */
  22. httpAddHeaderString(stream, "Content-Type", "text/html");
  23. httpSetHeaderString(stream, "Cache-Control", "no-cache");
  24. httpWrite(q, "<html><title>simpleAction</title><body>\r\n");
  25. httpWrite(q, "<p>Name: %s</p>\n", httpGetParam(stream, "name", "unspecified"));
  26. httpWrite(q, "<p>Address: %s</p>\n", httpGetParam(stream, "address", "unspecified"));
  27. httpWrite(q, "</body></html>\r\n");
  28. /*
  29. Call finalize output and close the request.
  30. Delay closing if you want to do asynchronous output and close later.
  31. */
  32. httpFinalize(stream);
  33. #if POSSIBLE
  34. /*
  35. Useful things to do in actions
  36. */
  37. httpRedirect(stream, 302, "/other-uri");
  38. httpError(stream, 409, "My message : %d", 5);
  39. #endif
  40. }
  41. /*
  42. Create a simple stand-alone web server
  43. */
  44. int main(int argc, char **argv, char **envp)
  45. {
  46. Mpr *mpr;
  47. int rc;
  48. rc = MPR_ERR_CANT_CREATE;
  49. if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) {
  50. mprError("Cannot create runtime");
  51. return -1;
  52. }
  53. if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) == 0) {
  54. mprError("Cannot create the HTTP services");
  55. return -1;
  56. }
  57. mprStart();
  58. if (maParseConfig("appweb.conf") < 0) {
  59. mprError("Cannot parse the config file %s", "appweb.conf");
  60. return -1;
  61. }
  62. httpDefineAction("/action/myaction", myaction);
  63. if (httpStartEndpoints() < 0) {
  64. mprError("Cannot start the web server");
  65. return -1;
  66. }
  67. mprServiceEvents(-1, 0);
  68. mprDestroy();
  69. return 0;
  70. }
  71. /*
  72. Copyright (c) Embedthis Software. All Rights Reserved.
  73. This software is distributed under commercial and open source licenses.
  74. You may use the Embedthis Open Source license or you may acquire a
  75. commercial license from Embedthis Software. You agree to be fully bound
  76. by the terms of either license. Consult the LICENSE.md distributed with
  77. this software for full details and other copyrights.
  78. */