cppHandler.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. cppHandler.cpp - Create a simple AppWeb request handler in C++
  3. This sample demonstrates creating a request handler to process requests.
  4. Copyright (c) All Rights Reserved. See copyright notice at the bottom of the file.
  5. */
  6. /******************************* Includes *****************************/
  7. #include "appweb.h"
  8. /********************************* Code *******************************/
  9. /*
  10. Run the handler. This is called when all input data has been received.
  11. */
  12. static void readySimple(HttpQueue *q)
  13. {
  14. HttpStream *stream;
  15. stream = q->stream;
  16. httpSetHeaderString(stream, "Custom-Date", stream->http->currentDate);
  17. /*
  18. Generate some dynamic data. If you generate a lot, this will buffer up to a configured maximum.
  19. If that limit is exceeded, the packet will be sent downstream and the response headers will be created.
  20. */
  21. httpWrite(q, "Hello World\n");
  22. /*
  23. Call finalize when the response to the client is complete. Call httpFlushOutput if the response is
  24. incomplete and you wish to immediately send any buffered output.
  25. */
  26. httpFinalize(stream);
  27. }
  28. static void incomingSimple(HttpQueue *q, HttpPacket *packet)
  29. {
  30. /*
  31. Do something with the incoming data in packet and then free the packet.
  32. */
  33. if (packet->content) {
  34. mprLog("debug", 0, "Data in packet is %s", mprGetBufStart(packet->content));
  35. }
  36. }
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /*
  41. Module load initialization. This is called when the module is first loaded. The module name is "Simple".
  42. */
  43. int httpSimpleHandlerInit(Http *http, MprModule *module)
  44. {
  45. HttpStage *stage;
  46. if ((stage = httpCreateHandler("simpleHandler", module)) == 0) {
  47. return MPR_ERR_CANT_CREATE;
  48. }
  49. stage->ready = readySimple;
  50. stage->incoming = incomingSimple;
  51. mprLog("debug", 0, "Loaded SimpleHandler");
  52. return 0;
  53. }
  54. #ifdef __cplusplus
  55. }
  56. #endif
  57. /*
  58. Copyright (c) Embedthis Software. All Rights Reserved.
  59. This software is distributed under commercial and open source licenses.
  60. You may use the Embedthis Open Source license or you may acquire a
  61. commercial license from Embedthis Software. You agree to be fully bound
  62. by the terms of either license. Consult the LICENSE.md distributed with
  63. this software for full details and other copyrights.
  64. */