CURLMOPT_PUSHFUNCTION.3 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .\"
  25. .TH CURLMOPT_PUSHFUNCTION 3 "1 Jun 2015" libcurl libcurl
  26. .SH NAME
  27. CURLMOPT_PUSHFUNCTION \- callback that approves or denies server pushes
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/curl.h>
  31. int curl_push_callback(CURL *parent,
  32. CURL *easy,
  33. size_t num_headers,
  34. struct curl_pushheaders *headers,
  35. void *clientp);
  36. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_PUSHFUNCTION,
  37. curl_push_callback func);
  38. .fi
  39. .SH DESCRIPTION
  40. This callback gets called when a new HTTP/2 stream is being pushed by the
  41. server (using the PUSH_PROMISE frame). If no push callback is set, all offered
  42. pushes will be denied automatically.
  43. .SH CALLBACK DESCRIPTION
  44. The callback gets its arguments like this:
  45. \fIparent\fP is the handle of the stream on which this push arrives. The new
  46. handle has been duplicated from the parent, meaning that it has gotten all its
  47. options inherited. It is then up to the application to alter any options if
  48. desired.
  49. \fIeasy\fP is a newly created handle that represents this upcoming transfer.
  50. \fInum_headers\fP is the number of name+value pairs that was received and can
  51. be accessed
  52. \fIheaders\fP is a handle used to access push headers using the accessor
  53. functions described below. This only accesses and provides the PUSH_PROMISE
  54. headers, the normal response headers will be provided in the header callback
  55. as usual.
  56. \fIclientp\fP is the pointer set with \fICURLMOPT_PUSHDATA(3)\fP
  57. If the callback returns CURL_PUSH_OK, the 'easy' handle will be added to the
  58. multi handle, the callback must not do that by itself.
  59. The callback can access PUSH_PROMISE headers with two accessor
  60. functions. These functions can only be used from within this callback and they
  61. can only access the PUSH_PROMISE headers: \fIcurl_pushheader_byname(3)\fP and
  62. \fIcurl_pushheader_bynum(3)\fP. The normal response headers will be passed to
  63. the header callback for pushed streams just as for normal streams.
  64. The header fields can also be accessed with \fIcurl_easy_header(3)\fP,
  65. introduced in later libcurl versions.
  66. .SH CALLBACK RETURN VALUE
  67. .IP "CURL_PUSH_OK (0)"
  68. The application has accepted the stream and it can now start receiving data,
  69. the ownership of the CURL handle has been taken over by the application.
  70. .IP "CURL_PUSH_DENY (1)"
  71. The callback denies the stream and no data for this will reach the
  72. application, the easy handle will be destroyed by libcurl.
  73. .IP "CURL_PUSH_ERROROUT (2)"
  74. Returning this will reject the pushed stream and return an error back on the
  75. parent stream making it get closed with an error. (Added in 7.72.0)
  76. .IP *
  77. All other return codes are reserved for future use.
  78. .SH DEFAULT
  79. NULL, no callback
  80. .SH PROTOCOLS
  81. HTTP(S) (HTTP/2 only)
  82. .SH EXAMPLE
  83. .nf
  84. /* only allow pushes for file names starting with "push-" */
  85. int push_callback(CURL *parent,
  86. CURL *easy,
  87. size_t num_headers,
  88. struct curl_pushheaders *headers,
  89. void *clientp)
  90. {
  91. char *headp;
  92. int *transfers = (int *)clientp;
  93. FILE *out;
  94. headp = curl_pushheader_byname(headers, ":path");
  95. if(headp && !strncmp(headp, "/push-", 6)) {
  96. fprintf(stderr, "The PATH is %s\\n", headp);
  97. /* save the push here */
  98. out = fopen("pushed-stream", "wb");
  99. /* write to this file */
  100. curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
  101. (*transfers)++; /* one more */
  102. return CURL_PUSH_OK;
  103. }
  104. return CURL_PUSH_DENY;
  105. }
  106. curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, push_callback);
  107. curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &counter);
  108. .fi
  109. .SH AVAILABILITY
  110. Added in 7.44.0
  111. .SH RETURN VALUE
  112. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  113. .SH "SEE ALSO"
  114. .BR CURLMOPT_PUSHDATA "(3), " CURLMOPT_PIPELINING "(3), " CURLOPT_PIPEWAIT "(3), "
  115. .BR RFC 7540