httpcli.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_CORE_LIB_HTTP_HTTPCLI_H
  34. #define GRPC_CORE_LIB_HTTP_HTTPCLI_H
  35. /* We currently need this at the top of the file if we import some iomgr
  36. headers because if we are building with libuv, those headers will include
  37. uv.h, which needs to be included before other system headers */
  38. #include "src/core/lib/iomgr/port.h"
  39. #include <stddef.h>
  40. #include <grpc/support/time.h>
  41. #include "src/core/lib/http/parser.h"
  42. #include "src/core/lib/iomgr/endpoint.h"
  43. #include "src/core/lib/iomgr/iomgr_internal.h"
  44. #include "src/core/lib/iomgr/polling_entity.h"
  45. #include "src/core/lib/iomgr/pollset_set.h"
  46. /* User agent this library reports */
  47. #define GRPC_HTTPCLI_USER_AGENT "grpc-httpcli/0.0"
  48. /* Tracks in-progress http requests
  49. TODO(ctiller): allow caching and capturing multiple requests for the
  50. same content and combining them */
  51. typedef struct grpc_httpcli_context {
  52. grpc_pollset_set *pollset_set;
  53. } grpc_httpcli_context;
  54. typedef struct {
  55. const char *default_port;
  56. void (*handshake)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint,
  57. const char *host, gpr_timespec deadline,
  58. void (*on_done)(grpc_exec_ctx *exec_ctx, void *arg,
  59. grpc_endpoint *endpoint));
  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. /* The host name to connect to */
  66. char *host;
  67. /* The host to verify in the SSL handshake (or NULL) */
  68. char *ssl_host_override;
  69. /* The main part of the request
  70. The following headers are supplied automatically and MUST NOT be set here:
  71. Host, Connection, User-Agent */
  72. grpc_http_request http;
  73. /* handshaker to use ssl for the request */
  74. const grpc_httpcli_handshaker *handshaker;
  75. } grpc_httpcli_request;
  76. /* Expose the parser response type as a httpcli response too */
  77. typedef struct grpc_http_response grpc_httpcli_response;
  78. void grpc_httpcli_context_init(grpc_httpcli_context *context);
  79. void grpc_httpcli_context_destroy(grpc_httpcli_context *context);
  80. /* Asynchronously perform a HTTP GET.
  81. 'context' specifies the http context under which to do the get
  82. 'pollset' indicates a grpc_pollset that is interested in the result
  83. of the get - work on this pollset may be used to progress the get
  84. operation
  85. 'request' contains request parameters - these are caller owned and can be
  86. destroyed once the call returns
  87. 'deadline' contains a deadline for the request (or gpr_inf_future)
  88. 'on_response' is a callback to report results to (and 'user_data' is a user
  89. supplied pointer to pass to said call) */
  90. void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
  91. grpc_polling_entity *pollent,
  92. const grpc_httpcli_request *request,
  93. gpr_timespec deadline, grpc_closure *on_complete,
  94. grpc_httpcli_response *response);
  95. /* Asynchronously perform a HTTP POST.
  96. 'context' specifies the http context under which to do the post
  97. 'pollset' indicates a grpc_pollset that is interested in the result
  98. of the post - work on this pollset may be used to progress the post
  99. operation
  100. 'request' contains request parameters - these are caller owned and can be
  101. destroyed once the call returns
  102. 'body_bytes' and 'body_size' specify the payload for the post.
  103. When there is no body, pass in NULL as body_bytes.
  104. 'deadline' contains a deadline for the request (or gpr_inf_future)
  105. 'em' points to a caller owned event manager that must be alive for the
  106. lifetime of the request
  107. 'on_response' is a callback to report results to (and 'user_data' is a user
  108. supplied pointer to pass to said call)
  109. Does not support ?var1=val1&var2=val2 in the path. */
  110. void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
  111. grpc_polling_entity *pollent,
  112. const grpc_httpcli_request *request,
  113. const char *body_bytes, size_t body_size,
  114. gpr_timespec deadline, grpc_closure *on_complete,
  115. grpc_httpcli_response *response);
  116. /* override functions return 1 if they handled the request, 0 otherwise */
  117. typedef int (*grpc_httpcli_get_override)(grpc_exec_ctx *exec_ctx,
  118. const grpc_httpcli_request *request,
  119. gpr_timespec deadline,
  120. grpc_closure *on_complete,
  121. grpc_httpcli_response *response);
  122. typedef int (*grpc_httpcli_post_override)(
  123. grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request,
  124. const char *body_bytes, size_t body_size, gpr_timespec deadline,
  125. grpc_closure *on_complete, grpc_httpcli_response *response);
  126. void grpc_httpcli_set_override(grpc_httpcli_get_override get,
  127. grpc_httpcli_post_override post);
  128. #endif /* GRPC_CORE_LIB_HTTP_HTTPCLI_H */