chunked_example.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2015 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 chunked_example.pp (original: chunked_example.c)
  18. * @brief example for generating chunked encoding with libmicrohttpd
  19. * @author Christian Grothoff / Silvio Clécio / Gilson Nunes
  20. *)
  21. program chunked_example;
  22. {$mode objfpc}{$H+}
  23. uses
  24. sysutils, libmicrohttpd;
  25. function callback(cls: Pointer; pos: cuint64; buf: Pcchar;
  26. max: size_t): ssize_t; cdecl;
  27. begin
  28. Result := MHD_CONTENT_READER_END_OF_STREAM;
  29. end;
  30. function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
  31. method: Pcchar; version: Pcchar; upload_data: Pcchar;
  32. upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  33. const
  34. aptr: cint = 0;
  35. var
  36. response: PMHD_Response;
  37. ret: cint;
  38. begin
  39. if 0 <> strcomp(method, 'GET') then
  40. Exit(MHD_NO);
  41. if @aptr <> ptr^ then
  42. begin
  43. ptr^ := @aptr;
  44. Exit(MHD_YES);
  45. end;
  46. ptr^ := nil;
  47. response := MHD_create_response_from_callback(UInt64(MHD_SIZE_UNKNOWN), 1024,
  48. @callback, nil, nil);
  49. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  50. MHD_destroy_response(response);
  51. Result := ret;
  52. end;
  53. var
  54. d: PMHD_Daemon;
  55. begin
  56. if argc <> 2 then
  57. begin
  58. WriteLn(argv[0], ' PORT');
  59. Halt(1);
  60. end;
  61. d := MHD_start_daemon(// MHD_USE_SELECT_INTERNALLY or MHD_USE_DEBUG or MHD_USE_POLL,
  62. MHD_USE_SELECT_INTERNALLY or MHD_USE_DEBUG,
  63. // MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG or MHD_USE_POLL,
  64. // MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG,
  65. StrToInt(argv[1]), nil, nil, @ahc_echo, nil,
  66. MHD_OPTION_CONNECTION_TIMEOUT, 120, MHD_OPTION_END);
  67. if d = nil then
  68. Halt(1);
  69. ReadLn;
  70. MHD_stop_daemon(d);
  71. end.