action.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. action.c -- Action handler
  3. This module implements the /action handler. It is a simple binding of URIs to C functions.
  4. This enables a very high performance implementation with easy parsing and decoding of query
  5. strings and posted data.
  6. Copyright (c) All Rights Reserved. See details at the end of the file.
  7. */
  8. /*********************************** Includes *********************************/
  9. #include "goahead.h"
  10. /************************************ Locals **********************************/
  11. static WebsHash actionTable = -1; /* Symbol table for actions */
  12. /************************************* Code ***********************************/
  13. /*
  14. Process an action request. Returns 1 always to indicate it handled the URL
  15. Return true to indicate the request was handled, even for errors.
  16. */
  17. static bool actionHandler(Webs *wp)
  18. {
  19. WebsKey *sp;
  20. char actionBuf[ME_GOAHEAD_LIMIT_URI + 1];
  21. char *cp, *actionName;
  22. WebsAction fn;
  23. char *index = websGetVar(wp, "index", NULL);
  24. // printf("---1111111111111111111111111111> wp.index = %s\n", index);
  25. uint8_t indexInt = atoi(index);
  26. wp->index = indexInt;
  27. assert(websValid(wp));
  28. assert(actionTable >= 0);
  29. /*
  30. Extract the action name
  31. */
  32. scopy(actionBuf, sizeof(actionBuf), wp->path);
  33. if ((actionName = strchr(&actionBuf[1], '/')) == NULL) {
  34. websError(wp, HTTP_CODE_NOT_FOUND, "Missing action name");
  35. return 1;
  36. }
  37. actionName++;
  38. if ((cp = strchr(actionName, '/')) != NULL) {
  39. *cp = '\0';
  40. }
  41. /*
  42. Lookup the C action function first and then try tcl (no javascript support yet).
  43. */
  44. sp = hashLookup(actionTable, actionName);
  45. if (sp == NULL) {
  46. websError(wp, HTTP_CODE_NOT_FOUND, "Action %s is not defined", actionName);
  47. } else {
  48. fn = (WebsAction) sp->content.value.symbol;
  49. assert(fn);
  50. if (fn) {
  51. #if ME_GOAHEAD_LEGACY
  52. (*((WebsProc) fn))((void*) wp, actionName, wp->query);
  53. #else
  54. (*fn)((void*) wp);
  55. #endif
  56. }
  57. }
  58. return 1;
  59. }
  60. /*
  61. Define a function in the "action" map space
  62. */
  63. PUBLIC int websDefineAction(cchar *name, void *fn)
  64. {
  65. assert(name && *name);
  66. assert(fn);
  67. if (fn == NULL) {
  68. return -1;
  69. }
  70. hashEnter(actionTable, (char*) name, valueSymbol(fn), 0);
  71. return 0;
  72. }
  73. static void closeAction()
  74. {
  75. if (actionTable != -1) {
  76. hashFree(actionTable);
  77. actionTable = -1;
  78. }
  79. }
  80. PUBLIC void websActionOpen()
  81. {
  82. actionTable = hashCreate(WEBS_HASH_INIT);
  83. websDefineHandler("action", 0, actionHandler, closeAction, 0);
  84. }
  85. #if ME_GOAHEAD_LEGACY
  86. /*
  87. Don't use these routes. Use websWriteHeaders, websEndHeaders instead.
  88. Write a webs header. This is a convenience routine to write a common header for an action back to the browser.
  89. */
  90. PUBLIC void websHeader(Webs *wp)
  91. {
  92. assert(websValid(wp));
  93. websWriteHeaders(wp, -1, 0);
  94. websWriteEndHeaders(wp);
  95. websWrite(wp, "<html>\n");
  96. }
  97. PUBLIC void websFooter(Webs *wp)
  98. {
  99. assert(websValid(wp));
  100. websWrite(wp, "</html>\n");
  101. }
  102. #endif
  103. /*
  104. Copyright (c) Embedthis Software. All Rights Reserved.
  105. This software is distributed under commercial and open source licenses.
  106. You may use the Embedthis GoAhead open source license or you may acquire
  107. a commercial license from Embedthis Software. You agree to be fully bound
  108. by the terms of either license. Consult the LICENSE.md distributed with
  109. this software for full details and other copyrights.
  110. */