CURLMOPT_PUSHFUNCTION.3 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. .\" generated by cd2nroff 0.1 from CURLMOPT_PUSHFUNCTION.md
  2. .TH CURLMOPT_PUSHFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLMOPT_PUSHFUNCTION \- callback that approves or denies server pushes
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. int curl_push_callback(CURL *parent,
  9. CURL *easy,
  10. size_t num_headers,
  11. struct curl_pushheaders *headers,
  12. void *clientp);
  13. CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_PUSHFUNCTION,
  14. curl_push_callback func);
  15. .fi
  16. .SH DESCRIPTION
  17. This callback gets called when a new HTTP/2 stream is being pushed by the
  18. server (using the PUSH_PROMISE frame). If no push callback is set, all offered
  19. pushes are denied automatically.
  20. .SH CALLBACK DESCRIPTION
  21. The callback gets its arguments like this:
  22. \fIparent\fP is the handle of the stream on which this push arrives. The new
  23. handle has been duplicated from the parent, meaning that it has gotten all its
  24. options inherited. It is then up to the application to alter any options if
  25. desired.
  26. \fIeasy\fP is a newly created handle that represents this upcoming transfer.
  27. \fInum_headers\fP is the number of name+value pairs that was received and can
  28. be accessed
  29. \fIheaders\fP is a handle used to access push headers using the accessor
  30. functions described below. This only accesses and provides the PUSH_PROMISE
  31. headers, the normal response headers are provided in the header callback as
  32. usual.
  33. \fIclientp\fP is the pointer set with \fICURLMOPT_PUSHDATA(3)\fP
  34. If the callback returns CURL_PUSH_OK, the new easy handle is added to the
  35. multi handle, the callback must not do that by itself.
  36. The callback can access PUSH_PROMISE headers with two accessor
  37. functions. These functions can only be used from within this callback and they
  38. can only access the PUSH_PROMISE headers: \fIcurl_pushheader_byname(3)\fP and
  39. \fIcurl_pushheader_bynum(3)\fP. The normal response headers are passed to the
  40. header callback for pushed streams just as for normal streams.
  41. The header fields can also be accessed with \fIcurl_easy_header(3)\fP,
  42. introduced in later libcurl versions.
  43. .SH CALLBACK RETURN VALUE
  44. .IP "CURL_PUSH_OK (0)"
  45. The application has accepted the stream and it can now start receiving data,
  46. the ownership of the CURL handle has been taken over by the application.
  47. .IP "CURL_PUSH_DENY (1)"
  48. The callback denies the stream and no data reaches the application, the easy
  49. handle is destroyed by libcurl.
  50. .IP "CURL_PUSH_ERROROUT (2)"
  51. Returning this code rejects the pushed stream and returns an error back on the
  52. parent stream making it get closed with an error. (Added in 7.72.0)
  53. .IP *
  54. All other return codes are reserved for future use.
  55. .SH DEFAULT
  56. NULL, no callback
  57. .SH PROTOCOLS
  58. This functionality affects http only
  59. .SH EXAMPLE
  60. .nf
  61. #include <string.h>
  62. /* only allow pushes for filenames starting with "push-" */
  63. int push_callback(CURL *parent,
  64. CURL *easy,
  65. size_t num_headers,
  66. struct curl_pushheaders *headers,
  67. void *clientp)
  68. {
  69. char *headp;
  70. int *transfers = (int *)clientp;
  71. FILE *out;
  72. headp = curl_pushheader_byname(headers, ":path");
  73. if(headp && !strncmp(headp, "/push-", 6)) {
  74. fprintf(stderr, "The PATH is %s\\n", headp);
  75. /* save the push here */
  76. out = fopen("pushed-stream", "wb");
  77. /* write to this file */
  78. curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
  79. (*transfers)++; /* one more */
  80. return CURL_PUSH_OK;
  81. }
  82. return CURL_PUSH_DENY;
  83. }
  84. int main(void)
  85. {
  86. int counter;
  87. CURLM *multi = curl_multi_init();
  88. curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, push_callback);
  89. curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &counter);
  90. }
  91. .fi
  92. .SH AVAILABILITY
  93. Added in curl 7.44.0
  94. .SH RETURN VALUE
  95. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  96. .SH SEE ALSO
  97. .BR CURLMOPT_PIPELINING (3),
  98. .BR CURLMOPT_PUSHDATA (3),
  99. .BR CURLOPT_PIPEWAIT (3),
  100. .BR RFC 7540