refuse_post_example.pp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. (*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. *)
  16. (**
  17. * @file refuse_post_example.pp (Original: refuse_post_example.c)
  18. * @brief example for how to refuse a POST request properly
  19. * @author Christian Grothoff, Sebastian Gerhardt and Silvio Clécio
  20. *)
  21. program refuse_post_example;
  22. {$mode objfpc}{$H+}
  23. uses
  24. sysutils, libmicrohttpd;
  25. const
  26. askpage: Pcchar =
  27. '<html><body>'#10+
  28. 'Upload a file, please!<br>'#10+
  29. '<form action="/filepost" method="post" enctype="multipart/form-data">'#10+
  30. '<input name="file" type="file">'#10+
  31. '<input type="submit" value=" Send "></form>'#10+
  32. '</body></html>';
  33. BUSYPAGE: Pcchar = '<html><head><title>Webserver busy</title></head><body>We are too busy to process POSTs right now.</body></html>';
  34. function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
  35. method: Pcchar; version: Pcchar; upload_data: Pcchar;
  36. upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  37. const
  38. aptr: cint = 0;
  39. var
  40. me: Pcchar;
  41. response: PMHD_Response;
  42. ret: cint;
  43. begin
  44. me := cls;
  45. if (0 <> strcomp(method, 'GET')) and (0 <> strcomp(method, 'POST')) then
  46. Exit(MHD_NO); (* unexpected method *)
  47. if @aptr <> ptr^ then
  48. begin
  49. ptr^ := @aptr;
  50. (* always to busy for POST requests *)
  51. if 0 = strcomp(method, 'POST') then
  52. begin
  53. response := MHD_create_response_from_buffer(strlen(BUSYPAGE),
  54. Pointer(BUSYPAGE), MHD_RESPMEM_PERSISTENT);
  55. ret := MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE,
  56. response);
  57. MHD_destroy_response (response);
  58. Exit(ret);
  59. end;
  60. end;
  61. ptr^ := nil; (* reset when done *)
  62. response := MHD_create_response_from_buffer(strlen(me), Pointer(me),
  63. MHD_RESPMEM_PERSISTENT);
  64. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  65. MHD_destroy_response(response);
  66. Result := ret;
  67. end;
  68. var
  69. d: PMHD_Daemon;
  70. begin
  71. if argc <> 2 then
  72. begin
  73. WriteLn(argv[0], ' PORT');
  74. Halt(1);
  75. end;
  76. d := MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG,
  77. StrToInt(argv[1]), nil, nil, @ahc_echo, Pointer(askpage),
  78. MHD_OPTION_END);
  79. if d = nil then
  80. Halt(1);
  81. ReadLn;
  82. MHD_stop_daemon(d);
  83. end.