CURLMOPT_PUSHFUNCTION.3 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "February 07, 2023" "libcurl 7.88.1" "curl_multi_setopt options"
  26. .SH NAME
  27. CURLMOPT_PUSHFUNCTION \- callback that approves or denies server pushes
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/curl.h>
  31. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num);
  32. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *name);
  33. int curl_push_callback(CURL *parent,
  34. CURL *easy,
  35. size_t num_headers,
  36. struct curl_pushheaders *headers,
  37. void *clientp);
  38. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_PUSHFUNCTION,
  39. curl_push_callback func);
  40. .fi
  41. .SH DESCRIPTION
  42. This callback gets called when a new HTTP/2 stream is being pushed by the
  43. server (using the PUSH_PROMISE frame). If no push callback is set, all offered
  44. pushes will be denied automatically.
  45. .SH CALLBACK DESCRIPTION
  46. The callback gets its arguments like this:
  47. \fIparent\fP is the handle of the stream on which this push arrives. The new
  48. handle has been duplicated from the parent, meaning that it has gotten all its
  49. options inherited. It is then up to the application to alter any options if
  50. desired.
  51. \fIeasy\fP is a newly created handle that represents this upcoming transfer.
  52. \fInum_headers\fP is the number of name+value pairs that was received and can
  53. be accessed
  54. \fIheaders\fP is a handle used to access push headers using the accessor
  55. functions described below. This only accesses and provides the PUSH_PROMISE
  56. headers, the normal response headers will be provided in the header callback
  57. as usual.
  58. \fIclientp\fP is the pointer set with \fICURLMOPT_PUSHDATA(3)\fP
  59. If the callback returns CURL_PUSH_OK, the 'easy' handle will be added to the
  60. multi handle, the callback must not do that by itself.
  61. The callback can access PUSH_PROMISE headers with two accessor
  62. functions. These functions can only be used from within this callback and they
  63. can only access the PUSH_PROMISE headers. The normal response headers will be
  64. passed to the header callback for pushed streams just as for normal streams.
  65. .IP curl_pushheader_bynum
  66. Returns the header at index \fInum\fP (or NULL). The returned pointer points
  67. to a "name:value" string that will be freed when this callback returns.
  68. .IP curl_pushheader_byname
  69. Returns the value for the given header name (or NULL). This is a shortcut so
  70. that the application does not have to loop through all headers to find the one
  71. it is interested in. The data pointed will be freed when this callback
  72. returns. If more than one header field use the same name, this returns only
  73. the first one.
  74. .SH CALLBACK RETURN VALUE
  75. .IP "CURL_PUSH_OK (0)"
  76. The application has accepted the stream and it can now start receiving data,
  77. the ownership of the CURL handle has been taken over by the application.
  78. .IP "CURL_PUSH_DENY (1)"
  79. The callback denies the stream and no data for this will reach the
  80. application, the easy handle will be destroyed by libcurl.
  81. .IP "CURL_PUSH_ERROROUT (2)"
  82. Returning this will reject the pushed stream and return an error back on the
  83. parent stream making it get closed with an error. (Added in 7.72.0)
  84. .IP *
  85. All other return codes are reserved for future use.
  86. .SH DEFAULT
  87. NULL, no callback
  88. .SH PROTOCOLS
  89. HTTP(S) (HTTP/2 only)
  90. .SH EXAMPLE
  91. .nf
  92. /* only allow pushes for file names starting with "push-" */
  93. int push_callback(CURL *parent,
  94. CURL *easy,
  95. size_t num_headers,
  96. struct curl_pushheaders *headers,
  97. void *clientp)
  98. {
  99. char *headp;
  100. int *transfers = (int *)clientp;
  101. FILE *out;
  102. headp = curl_pushheader_byname(headers, ":path");
  103. if(headp && !strncmp(headp, "/push-", 6)) {
  104. fprintf(stderr, "The PATH is %s\\n", headp);
  105. /* save the push here */
  106. out = fopen("pushed-stream", "wb");
  107. /* write to this file */
  108. curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
  109. (*transfers)++; /* one more */
  110. return CURL_PUSH_OK;
  111. }
  112. return CURL_PUSH_DENY;
  113. }
  114. curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, push_callback);
  115. curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &counter);
  116. .fi
  117. .SH AVAILABILITY
  118. Added in 7.44.0
  119. .SH RETURN VALUE
  120. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  121. .SH "SEE ALSO"
  122. .BR CURLMOPT_PUSHDATA "(3), " CURLMOPT_PIPELINING "(3), " CURLOPT_PIPEWAIT "(3), "
  123. .BR RFC 7540