ne_session.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. HTTP session handling
  3. Copyright (C) 1999-2009, Joe Orton <[email protected]>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  15. MA 02111-1307, USA
  16. */
  17. #ifndef NE_SESSION_H
  18. #define NE_SESSION_H 1
  19. #include <sys/types.h>
  20. #include "ne_ssl.h"
  21. #include "ne_uri.h" /* for ne_uri */
  22. #include "ne_defs.h"
  23. #include "ne_socket.h"
  24. NE_BEGIN_DECLS
  25. typedef struct ne_session_s ne_session;
  26. /* Create a session to the given server, using the given scheme. If
  27. * "https" is passed as the scheme, SSL will be used to connect to the
  28. * server. */
  29. ne_session *ne_session_create(const char *scheme,
  30. const char *hostname, unsigned int port);
  31. /* Finish an HTTP session */
  32. void ne_session_destroy(ne_session *sess);
  33. /* Prematurely force the connection to be closed for the given
  34. * session. */
  35. void ne_close_connection(ne_session *sess);
  36. /* Configure an HTTP proxy server for the session. This function will
  37. * override (remove) any proxy servers previously configured, and must
  38. * be called before any requests are created using this session. */
  39. void ne_session_proxy(ne_session *sess,
  40. const char *hostname, unsigned int port);
  41. /* Configure a SOCKS proxy server which will be used for the session.
  42. * The SOCKS protocol version 'vers' will be used to contact the
  43. * proxy at given 'hostname' and 'port'.
  44. *
  45. * If SOCKSv4 or v4a are used, username must be non-NULL. For v5,
  46. * username may be NULL, in which case, password is ignored. If
  47. * username is non-NULL, password must also be non-NULL.
  48. *
  49. * This function will override (remove) any proxy servers previously
  50. * configured, and must be called before any requests are created
  51. * using this session. */
  52. void ne_session_socks_proxy(ne_session *sess, enum ne_sock_sversion vers,
  53. const char *hostname, unsigned int port,
  54. const char *username, const char *password);
  55. /* Configure use of proxy servers from any system-wide default sources
  56. * which are configured at build time. This function will override
  57. * (remove) any proxy servers previously configured, and must be
  58. * called before any requests are created using this session. The
  59. * 'flags' parameter must be zero. */
  60. void ne_session_system_proxy(ne_session *sess, unsigned int flags);
  61. /* Defined session flags: */
  62. typedef enum ne_session_flag_e {
  63. NE_SESSFLAG_PERSIST = 0, /* disable this flag to prevent use of
  64. * persistent connections. */
  65. NE_SESSFLAG_ICYPROTO, /* enable this flag to enable support for
  66. * non-HTTP ShoutCast-style "ICY" responses. */
  67. NE_SESSFLAG_SSLv2, /* disable this flag to disable support for
  68. * SSLv2, if supported by the SSL library. */
  69. NE_SESSFLAG_RFC4918, /* enable this flag to enable support for
  70. * RFC4918-only WebDAV features; losing
  71. * backwards-compatibility with RFC2518
  72. * servers. */
  73. NE_SESSFLAG_CONNAUTH, /* enable this flag if an awful, broken,
  74. * RFC-violating, connection-based HTTP
  75. * authentication scheme is in use. */
  76. NE_SESSFLAG_TLS_SNI, /* disable this flag to disable use of the
  77. * TLS Server Name Indication extension. */
  78. NE_SESSFLAG_EXPECT100, /* enable this flag to enable the flag
  79. * NE_REQFLAG_EXPECT100 for new requests. */
  80. NE_SESSFLAG_SHAREPOINT, /* this flag enables various workarounds
  81. * to improve interoperability with
  82. * SharePoint */
  83. #ifdef WINSCP
  84. NE_SESSFLAG_LIBERAL_ESCAPING,
  85. #endif
  86. NE_SESSFLAG_LAST /* enum sentinel value */
  87. } ne_session_flag;
  88. /* Set a new value for a particular session flag. */
  89. void ne_set_session_flag(ne_session *sess, ne_session_flag flag, int value);
  90. /* Return 0 if the given flag is not set, >0 it is set, or -1 if the
  91. * flag is not supported. */
  92. int ne_get_session_flag(ne_session *sess, ne_session_flag flag);
  93. /* Bypass the normal name resolution; force the use of specific set of
  94. * addresses for this session, addrs[0]...addrs[n-1]. The 'addrs'
  95. * array and pointed-to objects must remain valid until the session is
  96. * destroyed. This function will override (remove) any proxy servers
  97. * previously configured, and must be called before any requests are
  98. * created using this session. */
  99. void ne_set_addrlist(ne_session *sess, const ne_inet_addr **addrs, size_t n);
  100. /* Bypass the normal name resolution; force the use of specific set of
  101. * addresses for this session, addrs[0]...addrs[n-1]. The 'addrs'
  102. * array and pointed-to objects must remain valid until the session is
  103. * destroyed. This function will override (remove) any proxy servers
  104. * previously configured, and must be called before any requests are
  105. * created using this session. Port number 'port' will be used
  106. * instead of the "real" session port, to connect to the proxy. */
  107. void ne_set_addrlist2(ne_session *sess, unsigned int port,
  108. const ne_inet_addr **addrs, size_t n);
  109. /* Bind connections to the specified local address. If the address
  110. * determined for the remote host has a different family (type) to
  111. * 'addr', 'addr' will be ignored. The 'addr' object must remain
  112. * valid until the session is destroyed. */
  113. void ne_set_localaddr(ne_session *sess, const ne_inet_addr *addr);
  114. /* DEPRECATED: Progress callback. */
  115. typedef void (*ne_progress)(void *userdata, ne_off_t progress, ne_off_t total);
  116. /* DEPRECATED API: Set a progress callback for the session; this is
  117. * deprecated in favour of ne_set_notifier(). The progress callback
  118. * is invoked for after each block of the request and response body to
  119. * indicate request and response progress (there is no way to
  120. * distinguish between the two using this interface alone).
  121. * If progress is NULL, any existing callback is deregistered and will
  122. * no longer be invoked.
  123. *
  124. * NOTE: Use of this interface is mutually exclusive with the use of
  125. * ne_set_notifier(). A call to ne_set_progress() removes the
  126. * notifier callback, and vice versa. */
  127. void ne_set_progress(ne_session *sess, ne_progress progress, void *userdata);
  128. /* Store an opaque context for the session, 'priv' is returned by a
  129. * call to ne_session_get_private with the same ID. */
  130. void ne_set_session_private(ne_session *sess, const char *id, void *priv);
  131. void *ne_get_session_private(ne_session *sess, const char *id);
  132. /* Status event type. NOTE: More event types may be added in
  133. * subsequent releases, so callers must ignore unknown status types
  134. * for forwards-compatibility. */
  135. typedef enum {
  136. ne_status_lookup = 0, /* looking up hostname */
  137. ne_status_connecting, /* connecting to host */
  138. ne_status_connected, /* connected to host */
  139. ne_status_sending, /* sending a request body */
  140. ne_status_recving, /* receiving a response body */
  141. ne_status_disconnected /* disconnected from host */
  142. } ne_session_status;
  143. /* Status event information union; the relevant structure within
  144. * corresponds to the event type. WARNING: the size of this union is
  145. * not limited by ABI constraint; it may be extended with additional
  146. * members of different size, or existing members may be extended. */
  147. typedef union ne_session_status_info_u {
  148. struct /* ne_status_lookup */ {
  149. /* The hostname which is being resolved: */
  150. const char *hostname;
  151. } lu;
  152. struct /* ne_status_connecting */ {
  153. /* The hostname and network address to which a connection
  154. * attempt is being made: */
  155. const char *hostname;
  156. const ne_inet_addr *address;
  157. } ci;
  158. struct /* ne_status_connected, ne_status_disconnected */ {
  159. /* The hostname to which a connection has just been
  160. * established or closed: */
  161. const char *hostname;
  162. } cd;
  163. struct /* ne_status_sending and ne_status_recving */ {
  164. /* Request/response body transfer progress; if total == -1,
  165. * the total size is unknown; otherwise, total gives the total
  166. * number of bytes which will be transferred. progress gives
  167. * the number of bytes transferred so far. */
  168. ne_off_t progress, total;
  169. } sr;
  170. } ne_session_status_info;
  171. /* Callback invoked to notify a new session status event, given by the
  172. * 'status' argument. On invocation, the contents of exactly one of
  173. * the structures in the info union will be valid, as indicated
  174. * above. */
  175. typedef void (*ne_notify_status)(void *userdata, ne_session_status status,
  176. const ne_session_status_info *info);
  177. /* Set a status notification callback for the session, to report
  178. * session status events. Only one notification callback per session
  179. * can be registered; the most recent of successive calls to this
  180. * function takes effect. If status is NULL, any existing callback
  181. * is deregistered and will no longer be invoked.
  182. *
  183. * NOTE: Use of this interface is mutually exclusive with the use of
  184. * ne_set_progress(). A call to ne_set_notifier() removes the
  185. * progress callback, and vice versa. */
  186. void ne_set_notifier(ne_session *sess, ne_notify_status status, void *userdata);
  187. /* Certificate verification failures. */
  188. /* NE_SSL_NOTYETVALID: the certificate is not yet valid. */
  189. #define NE_SSL_NOTYETVALID (0x01)
  190. /* NE_SSL_EXPIRED: the certificate has expired. */
  191. #define NE_SSL_EXPIRED (0x02)
  192. /* NE_SSL_IDMISMATCH: the hostname for which the certificate was
  193. * issued does not match the hostname of the server; this could mean
  194. * that the connection is being intercepted. */
  195. #define NE_SSL_IDMISMATCH (0x04)
  196. /* NE_SSL_UNTRUSTED: the certificate authority which signed the server
  197. * certificate is not trusted: there is no indicatation the server is
  198. * who they claim to be: */
  199. #define NE_SSL_UNTRUSTED (0x08)
  200. /* NE_SSL_BADCHAIN: the certificate chain contained a certificate
  201. * other than the server cert which failed verification for a reason
  202. * other than lack of trust; for example, due to a CA cert being
  203. * outside its validity period. */
  204. #define NE_SSL_BADCHAIN (0x10)
  205. /* N.B.: 0x20 is reserved. */
  206. /* NE_SSL_REVOKED: the server certificate has been revoked by the
  207. * issuing authority. */
  208. #define NE_SSL_REVOKED (0x40)
  209. /* For purposes of forwards-compatibility, the bitmask of all
  210. * currently exposed failure bits is given as NE_SSL_FAILMASK. If the
  211. * expression (failures & ~NE_SSL_FAILMASK) is non-zero a failure type
  212. * is present which the application does not recognize but must treat
  213. * as a verification failure nonetheless. */
  214. #define NE_SSL_FAILMASK (0x5f)
  215. /* A callback which is used when server certificate verification is
  216. * needed. The reasons for verification failure are given in the
  217. * 'failures' parameter, which is a binary OR of one or more of the
  218. * above NE_SSL_* values. failures is guaranteed to be non-zero. The
  219. * callback must return zero to accept the certificate: a non-zero
  220. * return value will fail the SSL negotiation. */
  221. typedef int (*ne_ssl_verify_fn)(void *userdata, int failures,
  222. const ne_ssl_certificate *cert);
  223. /* Install a callback to handle server certificate verification. This
  224. * is required when the CA certificate is not known for the server
  225. * certificate, or the server cert has other verification problems. */
  226. void ne_ssl_set_verify(ne_session *sess, ne_ssl_verify_fn fn, void *userdata);
  227. /* Use the given client certificate for the session. The client cert
  228. * MUST be in the decrypted state, otherwise behaviour is undefined.
  229. * The 'clicert' object is duplicated internally so can be destroyed
  230. * by the caller. */
  231. void ne_ssl_set_clicert(ne_session *sess, const ne_ssl_client_cert *clicert);
  232. /* Indicate that the certificate 'cert' is trusted; the 'cert' object
  233. * is duplicated internally so can be destroyed by the caller. This
  234. * function has no effect for non-SSL sessions. */
  235. void ne_ssl_trust_cert(ne_session *sess, const ne_ssl_certificate *cert);
  236. /* If the SSL library provided a default set of CA certificates, trust
  237. * this set of CAs. */
  238. void ne_ssl_trust_default_ca(ne_session *sess);
  239. /* Callback used to load a client certificate on demand. If dncount
  240. * is > 0, the 'dnames' array dnames[0] through dnames[dncount-1]
  241. * gives the list of CA names which the server indicated were
  242. * acceptable. The callback should load an appropriate client
  243. * certificate and then pass it to 'ne_ssl_set_clicert'. */
  244. typedef void (*ne_ssl_provide_fn)(void *userdata, ne_session *sess,
  245. const ne_ssl_dname *const *dnames,
  246. int dncount);
  247. /* Register a function to be called when the server requests a client
  248. * certificate. */
  249. void ne_ssl_provide_clicert(ne_session *sess,
  250. ne_ssl_provide_fn fn, void *userdata);
  251. #ifdef WINSCP
  252. const char * ne_ssl_get_version(ne_session *sess);
  253. char * ne_ssl_get_cipher(ne_session *sess);
  254. struct ssl_st;
  255. void ne_init_ssl_session(struct ssl_st *ssl, ne_session *sess);
  256. #endif
  257. /* Set the timeout (in seconds) used when reading from a socket. The
  258. * timeout value must be greater than zero. */
  259. void ne_set_read_timeout(ne_session *sess, int timeout);
  260. /* Set the timeout (in seconds) used when making a connection. The
  261. * timeout value must be greater than zero. */
  262. void ne_set_connect_timeout(ne_session *sess, int timeout);
  263. /* Sets the user-agent string. neon/VERSION will be appended, to make
  264. * the full header "User-Agent: product neon/VERSION".
  265. * If this function is not called, the User-Agent header is not sent.
  266. * The product string must follow the RFC2616 format, i.e.
  267. * product = token ["/" product-version]
  268. * product-version = token
  269. * where token is any alpha-numeric-y string [a-zA-Z0-9]* */
  270. void ne_set_useragent(ne_session *sess, const char *product);
  271. #ifdef WINSCP
  272. void ne_set_realhost(ne_session *sess, const char *realhost);
  273. #endif
  274. /* Returns non-zero if next-hop server does not claim compliance to
  275. * HTTP/1.1 or later. */
  276. int ne_version_pre_http11(ne_session *sess);
  277. /* Returns the 'hostport' URI segment for the end-server, e.g.
  278. * "my.server.com:8080". */
  279. const char *ne_get_server_hostport(ne_session *sess);
  280. /* Returns the URL scheme being used for the current session, omitting
  281. * the trailing ':'; e.g. "http" or "https". */
  282. const char *ne_get_scheme(ne_session *sess);
  283. /* Sets the host, scheme, and port fields of the given URI structure
  284. * to that of the configured server and scheme for the session; host
  285. * and scheme are malloc-allocated. No other fields in the URI
  286. * structure are changed. */
  287. void ne_fill_server_uri(ne_session *sess, ne_uri *uri);
  288. /* If a proxy is configured, sets the host and port fields in the
  289. * given URI structure to that of the proxy. If multiple proxies are
  290. * configured, the active is used if any, otherwise the first. The
  291. * hostname is malloc-allocated. No other fields in the URI structure
  292. * are changed; if no proxy is configured or a non-HTTP proxy is in
  293. * use, no fields are changed. */
  294. void ne_fill_proxy_uri(ne_session *sess, ne_uri *uri);
  295. /* Set the error string for the session; takes printf-like format
  296. * string. */
  297. void ne_set_error(ne_session *sess, const char *format, ...)
  298. ne_attribute((format (printf, 2, 3)));
  299. /* Retrieve the error string for the session */
  300. const char *ne_get_error(ne_session *sess);
  301. NE_END_DECLS
  302. #endif /* NE_SESSION_H */