httpcli.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "src/core/iomgr/endpoint.h"
  38. #include "src/core/iomgr/pollset_set.h"
  39. /* User agent this library reports */
  40. #define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
  41. /* Maximum length of a header string of the form 'Key: Value\r\n' */
  42. #define GRPC_HTTPCLI_MAX_HEADER_LENGTH 4096
  43. /* A single header to be passed in a request */
  44. typedef struct grpc_httpcli_header
  45. {
  46. char *key;
  47. char *value;
  48. } grpc_httpcli_header;
  49. /* Tracks in-progress http requests
  50. TODO(ctiller): allow caching and capturing multiple requests for the
  51. same content and combining them */
  52. typedef struct grpc_httpcli_context
  53. {
  54. grpc_pollset_set pollset_set;
  55. } grpc_httpcli_context;
  56. typedef struct
  57. {
  58. const char *default_port;
  59. void (*handshake) (void *arg, grpc_endpoint * endpoint, const char *host, void (*on_done) (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * endpoint, grpc_closure_list * closure_list));
  60. } grpc_httpcli_handshaker;
  61. extern const grpc_httpcli_handshaker grpc_httpcli_plaintext;
  62. extern const grpc_httpcli_handshaker grpc_httpcli_ssl;
  63. /* A request */
  64. typedef struct grpc_httpcli_request
  65. {
  66. /* The host name to connect to */
  67. char *host;
  68. /* The path of the resource to fetch */
  69. char *path;
  70. /* Additional headers: count and key/values; the following are supplied
  71. automatically and MUST NOT be set here:
  72. Host, Connection, User-Agent */
  73. size_t hdr_count;
  74. grpc_httpcli_header *hdrs;
  75. /* handshaker to use ssl for the request */
  76. const grpc_httpcli_handshaker *handshaker;
  77. } grpc_httpcli_request;
  78. /* A response */
  79. typedef struct grpc_httpcli_response
  80. {
  81. /* HTTP status code */
  82. int status;
  83. /* Headers: count and key/values */
  84. size_t hdr_count;
  85. grpc_httpcli_header *hdrs;
  86. /* Body: length and contents; contents are NOT null-terminated */
  87. size_t body_length;
  88. char *body;
  89. } grpc_httpcli_response;
  90. /* Callback for grpc_httpcli_get and grpc_httpcli_post. */
  91. typedef void (*grpc_httpcli_response_cb) (grpc_exec_ctx * exec_ctx, void *user_data, const grpc_httpcli_response * response);
  92. void grpc_httpcli_context_init (grpc_httpcli_context * context);
  93. void grpc_httpcli_context_destroy (grpc_httpcli_context * context);
  94. /* Asynchronously perform a HTTP GET.
  95. 'context' specifies the http context under which to do the get
  96. 'pollset' indicates a grpc_pollset that is interested in the result
  97. of the get - work on this pollset may be used to progress the get
  98. operation
  99. 'request' contains request parameters - these are caller owned and can be
  100. destroyed once the call returns
  101. 'deadline' contains a deadline for the request (or gpr_inf_future)
  102. 'on_response' is a callback to report results to (and 'user_data' is a user
  103. supplied pointer to pass to said call) */
  104. void grpc_httpcli_get (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context, grpc_pollset * pollset, const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
  105. /* Asynchronously perform a HTTP POST.
  106. 'context' specifies the http context under which to do the post
  107. 'pollset' indicates a grpc_pollset that is interested in the result
  108. of the post - work on this pollset may be used to progress the post
  109. operation
  110. 'request' contains request parameters - these are caller owned and can be
  111. destroyed once the call returns
  112. 'body_bytes' and 'body_size' specify the payload for the post.
  113. When there is no body, pass in NULL as body_bytes.
  114. 'deadline' contains a deadline for the request (or gpr_inf_future)
  115. 'em' points to a caller owned event manager that must be alive for the
  116. lifetime of the request
  117. 'on_response' is a callback to report results to (and 'user_data' is a user
  118. supplied pointer to pass to said call)
  119. Does not support ?var1=val1&var2=val2 in the path. */
  120. void grpc_httpcli_post (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context, grpc_pollset * pollset, const grpc_httpcli_request * request, const char *body_bytes, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
  121. /* override functions return 1 if they handled the request, 0 otherwise */
  122. typedef int (*grpc_httpcli_get_override) (grpc_exec_ctx * exec_ctx, const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
  123. typedef int (*grpc_httpcli_post_override) (grpc_exec_ctx * exec_ctx, const grpc_httpcli_request * request, const char *body_bytes, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
  124. void grpc_httpcli_set_override (grpc_httpcli_get_override get, grpc_httpcli_post_override post);
  125. #endif /* GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_H */