CURLMOPT_PUSHFUNCTION.3 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2015, 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.haxx.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. .\" **************************************************************************
  22. .\"
  23. .TH CURLMOPT_PUSHFUNCTION 3 "February 03, 2016" "libcurl 7.61.1" "curl_multi_setopt options"
  24. .SH NAME
  25. CURLMOPT_PUSHFUNCTION \- callback that approves or denies server pushes
  26. .SH SYNOPSIS
  27. .nf
  28. #include <curl/curl.h>
  29. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num);
  30. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *name);
  31. int curl_push_callback(CURL *parent,
  32. CURL *easy,
  33. size_t num_headers,
  34. struct curl_pushheaders *headers,
  35. void *userp);
  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 duphandle()d from the parent, meaning that it has gotten all
  47. its options inherited. It is then up to the application to alter any options
  48. if 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. \fIuserp\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. The normal response headers will be
  62. passed to the header callback for pushed streams just as for normal streams.
  63. .IP curl_pushheader_bynum
  64. Returns the header at index 'num' (or NULL). The returned pointer points to a
  65. "name:value" string that will be freed when this callback returns.
  66. .IP curl_pushheader_byname
  67. Returns the value for the given header name (or NULL). This is a shortcut so
  68. that the application doesn't have to loop through all headers to find the one
  69. it is interested in. The data pointed will be freed when this callback
  70. returns. If more than one header field use the same name, this returns only
  71. the first one.
  72. .SH CALLBACK RETURN VALUE
  73. .IP "CURL_PUSH_OK (0)"
  74. The application has accepted the stream and it can now start receiving data,
  75. the ownership of the CURL handle has been taken over by the application.
  76. .IP "CURL_PUSH_DENY (1)"
  77. The callback denies the stream and no data for this will reach the
  78. application, the easy handle will be destroyed by libcurl.
  79. .IP *
  80. All other return codes are reserved for future use.
  81. .SH DEFAULT
  82. NULL, no callback
  83. .SH PROTOCOLS
  84. HTTP(S) (HTTP/2 only)
  85. .SH EXAMPLE
  86. .nf
  87. /* only allow pushes for file names starting with "push-" */
  88. int push_callback(CURL *parent,
  89. CURL *easy,
  90. size_t num_headers,
  91. struct curl_pushheaders *headers,
  92. void *userp)
  93. {
  94. char *headp;
  95. int *transfers = (int *)userp;
  96. FILE *out;
  97. headp = curl_pushheader_byname(headers, ":path");
  98. if(headp && !strncmp(headp, "/push-", 6)) {
  99. fprintf(stderr, "The PATH is %s\\n", headp);
  100. /* save the push here */
  101. out = fopen("pushed-stream", "wb");
  102. /* write to this file */
  103. curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
  104. (*transfers)++; /* one more */
  105. return CURL_PUSH_OK;
  106. }
  107. return CURL_PUSH_DENY;
  108. }
  109. curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, push_callback);
  110. curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &counter);
  111. .fi
  112. .SH AVAILABILITY
  113. Added in 7.44.0
  114. .SH RETURN VALUE
  115. Returns CURLM_OK if the option is supported, and CURLM_UNKNOWN_OPTION if not.
  116. .SH "SEE ALSO"
  117. .BR CURLMOPT_PUSHDATA "(3), " CURLMOPT_PIPELINING "(3), " CURLOPT_PIPEWAIT "(3), "
  118. .BR RFC 7540