req.xml 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <refentry id="refreq">
  2. <refmeta>
  3. <refentrytitle>ne_request_create</refentrytitle>
  4. <manvolnum>3</manvolnum>
  5. </refmeta>
  6. <refnamediv>
  7. <refname id="ne_request_create">ne_request_create</refname>
  8. <refname id="ne_request_dispatch">ne_request_dispatch</refname>
  9. <refname id="ne_request_destroy">ne_request_destroy</refname>
  10. <refpurpose>low-level HTTP request handling</refpurpose>
  11. </refnamediv>
  12. <refsynopsisdiv>
  13. <funcsynopsis>
  14. <funcsynopsisinfo>#include &lt;ne_request.h&gt;</funcsynopsisinfo>
  15. <funcprototype>
  16. <funcdef>ne_request *<function>ne_request_create</function></funcdef>
  17. <paramdef>ne_session *<parameter>session</parameter></paramdef>
  18. <paramdef>const char *<parameter>method</parameter></paramdef>
  19. <paramdef>const char *<parameter>target</parameter></paramdef>
  20. </funcprototype>
  21. <funcprototype>
  22. <funcdef>int <function>ne_request_dispatch</function></funcdef>
  23. <paramdef>ne_request *<parameter>req</parameter></paramdef>
  24. </funcprototype>
  25. <funcprototype>
  26. <funcdef>void <function>ne_request_destroy</function></funcdef>
  27. <paramdef>ne_request *<parameter>req</parameter></paramdef>
  28. </funcprototype>
  29. </funcsynopsis>
  30. </refsynopsisdiv>
  31. <refsect1>
  32. <title>Description</title>
  33. <para>The <type>ne_request</type> object represents an HTTP
  34. request and the associated response. The
  35. <function>ne_request_create</function> function creates a new
  36. request object for the given <parameter>session</parameter>.
  37. The target resource for the request is identified by the
  38. <parameter>target</parameter>, parameter, and the method to be
  39. performed on that resource via the
  40. <parameter>method</parameter> parameter.</para>
  41. <para>The <parameter>target</parameter> string used must conform to
  42. the <literal>request-target</literal> definition given in <ulink
  43. url="https://www.rfc-editor.org/rfc/rfc9112">RFC 9112</ulink>. Usually
  44. this will take the <literal>abolute-path</literal> form, which
  45. optionally includes a query string.</para>
  46. <para>To <emphasis>dispatch</emphasis> a request, and process the response, the
  47. <function>ne_request_dispatch</function> function can be used. An
  48. alternative is to use the (more complex, but more flexible)
  49. combination of the <function>ne_begin_request</function>,
  50. <function>ne_end_request</function>, and
  51. <function>ne_read_response_block</function> functions; see
  52. <function>ne_begin_request</function>. <emphasis>Dispatching</emphasis> a request may require
  53. multiple iterations of a request being sent and response received, for example
  54. if authentication is used (see <xref linkend="ne_set_server_auth"/>), or if a persistent
  55. connection times out; this is handled internally by <function>ne_request_dispatch</function>.</para>
  56. <para>To add extra headers in the request, the functions <xref
  57. linkend="ne_add_request_header"/> and <xref
  58. linkend="ne_print_request_header"/> can be used. To include a message
  59. body with the request, one of the functions
  60. <function>ne_set_request_body_buffer</function>, <function>ne_set_request_body_fd</function>, or
  61. <function>ne_set_request_body_provider</function> can be used.</para>
  62. <para>The return value of
  63. <function>ne_request_dispatch</function> indicates merely whether the
  64. request was sent and the response read successfully. To discover the
  65. result of the operation, <xref linkend="ne_get_status"/>, along with
  66. any processing of the response headers and message body.</para>
  67. <para>A request can only be dispatched once: calling
  68. <function>ne_request_dispatch</function> more than once on a
  69. single <type>ne_request</type> object produces undefined
  70. behaviour. Once all processing associated with the request
  71. object is complete, use the
  72. <function>ne_request_destroy</function> function to destroy
  73. the resources associated with it. Any subsequent use of the
  74. request object produces undefined behaviour.</para>
  75. <para>Request methods are assumed to be <ulink
  76. url="https://www.rfc-editor.org/rfc/rfc9110.html#name-idempotent-methods">idempotent</ulink>
  77. by default. For a request using a non-idempotent method such
  78. as <literal>POST</literal>, the
  79. <literal>NE_REQFLAG_IDEMPOTENT</literal> flag must be
  80. disabled using <xref linkend="ne_set_request_flag"/>.</para>
  81. </refsect1>
  82. <refsect1>
  83. <title>Return value</title>
  84. <para>The <function>ne_request_create</function> function
  85. returns a pointer to a request object (and never &null;).</para>
  86. <para>The <function>ne_request_dispatch</function> function
  87. returns zero if the request was dispatched successfully, and a
  88. non-zero error code otherwise.</para>
  89. </refsect1>
  90. <refsect1>
  91. <title>Notes</title>
  92. <para>The <parameter>path</parameter>,
  93. <parameter>method</parameter> and
  94. <parameter>target</parameter> parameters of
  95. <function>ne_request_create</function> are used directly in
  96. request data without validation, so must not be taken from
  97. untrusted sources. For example, allowing insertion of
  98. unescaped CR, LF or other control characters in these
  99. parameters may result in unexpected or insecure behaviour.</para>
  100. <para>&neon; does not impose any length restrictions on
  101. request input data.</para>
  102. </refsect1>
  103. <refsect1>
  104. <title>Errors</title>
  105. <variablelist>
  106. <varlistentry><term><errorcode>NE_ERROR</errorcode></term>
  107. <listitem>
  108. <simpara>Request failed (see session error string)</simpara>
  109. </listitem>
  110. </varlistentry>
  111. <varlistentry><term><errorcode>NE_LOOKUP</errorcode></term>
  112. <listitem>
  113. <simpara>The DNS lookup for the server (or proxy server) failed.</simpara>
  114. </listitem>
  115. </varlistentry>
  116. <varlistentry><term><errorcode>NE_AUTH</errorcode></term>
  117. <listitem>
  118. <simpara>Authentication failed on the server.</simpara>
  119. </listitem>
  120. </varlistentry>
  121. <varlistentry><term><errorcode>NE_PROXYAUTH</errorcode></term>
  122. <listitem>
  123. <simpara>Authentication failed on the proxy server.</simpara>
  124. </listitem>
  125. </varlistentry>
  126. <varlistentry><term><errorcode>NE_CONNECT</errorcode></term>
  127. <listitem>
  128. <simpara>A connection to the server could not be established.</simpara>
  129. </listitem>
  130. </varlistentry>
  131. <varlistentry><term><errorcode>NE_TIMEOUT</errorcode></term>
  132. <listitem>
  133. <simpara>A timeout occurred while waiting for the server to respond.</simpara>
  134. </listitem>
  135. </varlistentry>
  136. </variablelist>
  137. </refsect1>
  138. <refsect1>
  139. <title>Example</title>
  140. <para>An example of applying a <literal>MKCOL</literal>
  141. operation to the resource at the location
  142. <literal>http://www.example.com/foo/bar/</literal>:</para>
  143. <programlisting>ne_session *sess = ne_session_create("http", "www.example.com", 80);
  144. ne_request *req = ne_request_create(sess, "MKCOL", "/foo/bar/");
  145. if (ne_request_dispatch(req)) {
  146. printf("Request failed: %s\n", ne_get_error(sess));
  147. }
  148. ne_request_destroy(req);</programlisting>
  149. </refsect1>
  150. <refsect1>
  151. <title>See also</title>
  152. <para><xref linkend="ne_get_error"/>, <xref
  153. linkend="ne_set_error"/>, <xref linkend="ne_get_status"/>, <xref
  154. linkend="ne_add_request_header"/>, <xref
  155. linkend="ne_set_request_body_buffer"/>, <xref linkend="ne_set_request_flag"/>.</para>
  156. </refsect1>
  157. </refentry>