2
0

dual_stack_example.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. (*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007, 2012 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 dual_stack_example.pp (Original: dual_stack_example.c)
  18. * @brief how to use MHD with both IPv4 and IPv6 support (dual-stack)
  19. * @author Christian Grothoff / Silvio Clécio
  20. *)
  21. // To test it, just execute: $ curl -g -6 "http://[::1]:8888/"
  22. program dual_stack_example;
  23. {$mode objfpc}{$H+}
  24. uses
  25. sysutils, libmicrohttpd;
  26. const
  27. PAGE: Pcchar = '<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>';
  28. function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
  29. method: Pcchar; version: Pcchar; upload_data: Pcchar;
  30. upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  31. const
  32. aptr: cint = 0;
  33. var
  34. me: Pcchar;
  35. response: PMHD_Response;
  36. ret: cint;
  37. begin
  38. me := cls;
  39. if 0 <> strcomp(method, 'GET') then
  40. Exit(MHD_NO); (* unexpected method *)
  41. if @aptr <> ptr^ then
  42. begin
  43. (* do never respond on first call *)
  44. ptr^ := @aptr;
  45. Exit(MHD_YES);
  46. end;
  47. ptr^ := nil; (* reset when done *)
  48. response := MHD_create_response_from_buffer(strlen(me), Pointer(me),
  49. MHD_RESPMEM_PERSISTENT);
  50. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  51. MHD_destroy_response(response);
  52. Result := ret;
  53. end;
  54. var
  55. d: PMHD_Daemon;
  56. begin
  57. if argc <> 2 then
  58. begin
  59. WriteLn(argv[0], ' PORT');
  60. Halt(1);
  61. end;
  62. d := MHD_start_daemon(MHD_USE_SELECT_INTERNALLY or MHD_USE_DEBUG or
  63. MHD_USE_DUAL_STACK, StrToInt(argv[1]), nil, nil, @ahc_echo, PAGE,
  64. MHD_OPTION_CONNECTION_TIMEOUT, cuint(120), MHD_OPTION_END);
  65. ReadLn;
  66. MHD_stop_daemon(d);
  67. end.