2
0

querystring_example.pp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 querystring_example.pp (Original: querystring_example.c)
  18. * @brief example for how to get the query string from libmicrohttpd
  19. * Call with an URI ending with something like "?q=QUERY"
  20. * @author Christian Grothoff / Silvio Clécio
  21. *)
  22. program querystring_example;
  23. {$mode objfpc}{$H+}
  24. uses
  25. sysutils, cmem, ctypes, cutils, libmicrohttpd;
  26. const
  27. PAGE: Pcchar = '<html><head><title>libmicrohttpd demo</title></head><body>Query string for &quot;%s&quot; was &quot;%s&quot;</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. fmt: Pcchar;
  35. val: Pcchar;
  36. me: Pcchar;
  37. response: PMHD_Response;
  38. ret: cint;
  39. begin
  40. fmt := cls;
  41. if 0 <> strcomp(method, 'GET') then
  42. Exit(MHD_NO);
  43. if @aptr <> ptr^ then
  44. begin
  45. ptr^ := @aptr;
  46. Exit(MHD_YES);
  47. end;
  48. ptr^ := nil;
  49. val := MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, 'q');
  50. me := Malloc(snprintf(nil, 0, fmt, Pcchar('q'), val) + 1);
  51. if me = nil then
  52. Exit(MHD_NO);
  53. sprintf(me, fmt, Pcchar('q'), val);
  54. response := MHD_create_response_from_buffer(strlen(me), Pointer(me),
  55. MHD_RESPMEM_MUST_FREE);
  56. if response = nil then
  57. begin
  58. Free(me);
  59. Exit(MHD_NO);
  60. end;
  61. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  62. MHD_destroy_response(response);
  63. Result := ret;
  64. end;
  65. var
  66. d: PMHD_Daemon;
  67. begin
  68. if argc <> 2 then
  69. begin
  70. WriteLn(argv[0], ' PORT');
  71. Halt(1);
  72. end;
  73. d := MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG,
  74. StrToInt(argv[1]), nil, nil, @ahc_echo, PAGE, MHD_OPTION_END);
  75. if d = nil then
  76. Halt(1);
  77. ReadLn;
  78. MHD_stop_daemon(d);
  79. end.