httpcli.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #ifndef GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_H
  34. #define GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_H
  35. #include <stddef.h>
  36. #include <grpc/support/time.h>
  37. /* User agent this library reports */
  38. #define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
  39. /* Maximum length of a header string of the form 'Key: Value\r\n' */
  40. #define GRPC_HTTPCLI_MAX_HEADER_LENGTH 4096
  41. /* A single header to be passed in a request */
  42. typedef struct grpc_httpcli_header {
  43. char *key;
  44. char *value;
  45. } grpc_httpcli_header;
  46. /* A request */
  47. typedef struct grpc_httpcli_request {
  48. /* The host name to connect to */
  49. char *host;
  50. /* The path of the resource to fetch */
  51. char *path;
  52. /* Additional headers: count and key/values; the following are supplied
  53. automatically and MUST NOT be set here:
  54. Host, Connection, User-Agent */
  55. size_t hdr_count;
  56. grpc_httpcli_header *hdrs;
  57. /* whether to use ssl for the request */
  58. int use_ssl;
  59. } grpc_httpcli_request;
  60. /* A response */
  61. typedef struct grpc_httpcli_response {
  62. /* HTTP status code */
  63. int status;
  64. /* Headers: count and key/values */
  65. size_t hdr_count;
  66. grpc_httpcli_header *hdrs;
  67. /* Body: length and contents; contents are NOT null-terminated */
  68. size_t body_length;
  69. char *body;
  70. } grpc_httpcli_response;
  71. /* Callback for grpc_httpcli_get */
  72. typedef void (*grpc_httpcli_response_cb)(void *user_data,
  73. const grpc_httpcli_response *response);
  74. /* Asynchronously perform a HTTP GET.
  75. 'request' contains request parameters - these are caller owned and can be
  76. destroyed once the call returns
  77. 'deadline' contains a deadline for the request (or gpr_inf_future)
  78. 'em' points to a caller owned event manager that must be alive for the
  79. lifetime of the request
  80. 'on_response' is a callback to report results to (and 'user_data' is a user
  81. supplied pointer to pass to said call) */
  82. void grpc_httpcli_get(const grpc_httpcli_request *request,
  83. gpr_timespec deadline,
  84. grpc_httpcli_response_cb on_response, void *user_data);
  85. /* Asynchronously perform a HTTP POST.
  86. When there is no body, pass in NULL as body_bytes.
  87. Does not support ?var1=val1&var2=val2 in the path. */
  88. void grpc_httpcli_post(const grpc_httpcli_request *request,
  89. const char *body_bytes, size_t body_size,
  90. gpr_timespec deadline,
  91. grpc_httpcli_response_cb on_response, void *user_data);
  92. /* override functions return 1 if they handled the request, 0 otherwise */
  93. typedef int (*grpc_httpcli_get_override)(const grpc_httpcli_request *request,
  94. gpr_timespec deadline,
  95. grpc_httpcli_response_cb on_response,
  96. void *user_data);
  97. typedef int (*grpc_httpcli_post_override)(const grpc_httpcli_request *request,
  98. const char *body_bytes,
  99. size_t body_size,
  100. gpr_timespec deadline,
  101. grpc_httpcli_response_cb on_response,
  102. void *user_data);
  103. void grpc_httpcli_set_override(grpc_httpcli_get_override get,
  104. grpc_httpcli_post_override post);
  105. #endif /* GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_H */