ne_session.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. SE_SESSFLAG_SNDBUF,
  86. #endif
  87. NE_SESSFLAG_LAST /* enum sentinel value */
  88. } ne_session_flag;
  89. /* Set a new value for a particular session flag. */
  90. void ne_set_session_flag(ne_session *sess, ne_session_flag flag, int value);
  91. /* Return 0 if the given flag is not set, >0 it is set, or -1 if the
  92. * flag is not supported. */
  93. int ne_get_session_flag(ne_session *sess, ne_session_flag flag);
  94. /* Bypass the normal name resolution; force the use of specific set of
  95. * addresses for this session, addrs[0]...addrs[n-1]. The 'addrs'
  96. * array and pointed-to objects must remain valid until the session is
  97. * destroyed. This function will override (remove) any proxy servers
  98. * previously configured, and must be called before any requests are
  99. * created using this session. */
  100. void ne_set_addrlist(ne_session *sess, const ne_inet_addr **addrs, size_t n);
  101. /* Bypass the normal name resolution; force the use of specific set of
  102. * addresses for this session, addrs[0]...addrs[n-1]. The 'addrs'
  103. * array and pointed-to objects must remain valid until the session is
  104. * destroyed. This function will override (remove) any proxy servers
  105. * previously configured, and must be called before any requests are
  106. * created using this session. Port number 'port' will be used
  107. * instead of the "real" session port, to connect to the proxy. */
  108. void ne_set_addrlist2(ne_session *sess, unsigned int port,
  109. const ne_inet_addr **addrs, size_t n);
  110. /* Bind connections to the specified local address. If the address
  111. * determined for the remote host has a different family (type) to
  112. * 'addr', 'addr' will be ignored. The 'addr' object must remain
  113. * valid until the session is destroyed. */
  114. void ne_set_localaddr(ne_session *sess, const ne_inet_addr *addr);
  115. /* DEPRECATED: Progress callback. */
  116. typedef void (*ne_progress)(void *userdata, ne_off_t progress, ne_off_t total);
  117. /* DEPRECATED API: Set a progress callback for the session; this is
  118. * deprecated in favour of ne_set_notifier(). The progress callback
  119. * is invoked for after each block of the request and response body to
  120. * indicate request and response progress (there is no way to
  121. * distinguish between the two using this interface alone).
  122. * If progress is NULL, any existing callback is deregistered and will
  123. * no longer be invoked.
  124. *
  125. * NOTE: Use of this interface is mutually exclusive with the use of
  126. * ne_set_notifier(). A call to ne_set_progress() removes the
  127. * notifier callback, and vice versa. */
  128. void ne_set_progress(ne_session *sess, ne_progress progress, void *userdata);
  129. /* Store an opaque context for the session, 'priv' is returned by a
  130. * call to ne_session_get_private with the same ID. */
  131. void ne_set_session_private(ne_session *sess, const char *id, void *priv);
  132. void *ne_get_session_private(ne_session *sess, const char *id);
  133. /* Status event type. NOTE: More event types may be added in
  134. * subsequent releases, so callers must ignore unknown status types
  135. * for forwards-compatibility. */
  136. typedef enum {
  137. ne_status_lookup = 0, /* looking up hostname */
  138. ne_status_connecting, /* connecting to host */
  139. ne_status_connected, /* connected to host */
  140. ne_status_sending, /* sending a request body */
  141. ne_status_recving, /* receiving a response body */
  142. ne_status_disconnected /* disconnected from host */
  143. } ne_session_status;
  144. /* Status event information union; the relevant structure within
  145. * corresponds to the event type. WARNING: the size of this union is
  146. * not limited by ABI constraint; it may be extended with additional
  147. * members of different size, or existing members may be extended. */
  148. typedef union ne_session_status_info_u {
  149. struct /* ne_status_lookup */ {
  150. /* The hostname which is being resolved: */
  151. const char *hostname;
  152. } lu;
  153. struct /* ne_status_connecting */ {
  154. /* The hostname and network address to which a connection
  155. * attempt is being made: */
  156. const char *hostname;
  157. const ne_inet_addr *address;
  158. } ci;
  159. struct /* ne_status_connected, ne_status_disconnected */ {
  160. /* The hostname to which a connection has just been
  161. * established or closed: */
  162. const char *hostname;
  163. } cd;
  164. struct /* ne_status_sending and ne_status_recving */ {
  165. /* Request/response body transfer progress; if total == -1,
  166. * the total size is unknown; otherwise, total gives the total
  167. * number of bytes which will be transferred. progress gives
  168. * the number of bytes transferred so far. */
  169. ne_off_t progress, total;
  170. } sr;
  171. } ne_session_status_info;
  172. /* Callback invoked to notify a new session status event, given by the
  173. * 'status' argument. On invocation, the contents of exactly one of
  174. * the structures in the info union will be valid, as indicated
  175. * above. */
  176. typedef void (*ne_notify_status)(void *userdata, ne_session_status status,
  177. const ne_session_status_info *info);
  178. /* Set a status notification callback for the session, to report
  179. * session status events. Only one notification callback per session
  180. * can be registered; the most recent of successive calls to this
  181. * function takes effect. If status is NULL, any existing callback
  182. * is deregistered and will no longer be invoked.
  183. *
  184. * NOTE: Use of this interface is mutually exclusive with the use of
  185. * ne_set_progress(). A call to ne_set_notifier() removes the
  186. * progress callback, and vice versa. */
  187. void ne_set_notifier(ne_session *sess, ne_notify_status status, void *userdata);
  188. /* Certificate verification failures. */
  189. /* NE_SSL_NOTYETVALID: the certificate is not yet valid. */
  190. #define NE_SSL_NOTYETVALID (0x01)
  191. /* NE_SSL_EXPIRED: the certificate has expired. */
  192. #define NE_SSL_EXPIRED (0x02)
  193. /* NE_SSL_IDMISMATCH: the hostname for which the certificate was
  194. * issued does not match the hostname of the server; this could mean
  195. * that the connection is being intercepted. */
  196. #define NE_SSL_IDMISMATCH (0x04)
  197. /* NE_SSL_UNTRUSTED: the certificate authority which signed the server
  198. * certificate is not trusted: there is no indicatation the server is
  199. * who they claim to be: */
  200. #define NE_SSL_UNTRUSTED (0x08)
  201. /* NE_SSL_BADCHAIN: the certificate chain contained a certificate
  202. * other than the server cert which failed verification for a reason
  203. * other than lack of trust; for example, due to a CA cert being
  204. * outside its validity period. */
  205. #define NE_SSL_BADCHAIN (0x10)
  206. /* N.B.: 0x20 is reserved. */
  207. /* NE_SSL_REVOKED: the server certificate has been revoked by the
  208. * issuing authority. */
  209. #define NE_SSL_REVOKED (0x40)
  210. /* For purposes of forwards-compatibility, the bitmask of all
  211. * currently exposed failure bits is given as NE_SSL_FAILMASK. If the
  212. * expression (failures & ~NE_SSL_FAILMASK) is non-zero a failure type
  213. * is present which the application does not recognize but must treat
  214. * as a verification failure nonetheless. */
  215. #define NE_SSL_FAILMASK (0x5f)
  216. /* A callback which is used when server certificate verification is
  217. * needed. The reasons for verification failure are given in the
  218. * 'failures' parameter, which is a binary OR of one or more of the
  219. * above NE_SSL_* values. failures is guaranteed to be non-zero. The
  220. * callback must return zero to accept the certificate: a non-zero
  221. * return value will fail the SSL negotiation. */
  222. typedef int (*ne_ssl_verify_fn)(void *userdata, int failures,
  223. const ne_ssl_certificate *cert);
  224. /* Install a callback to handle server certificate verification. This
  225. * is required when the CA certificate is not known for the server
  226. * certificate, or the server cert has other verification problems. */
  227. void ne_ssl_set_verify(ne_session *sess, ne_ssl_verify_fn fn, void *userdata);
  228. /* Use the given client certificate for the session. The client cert
  229. * MUST be in the decrypted state, otherwise behaviour is undefined.
  230. * The 'clicert' object is duplicated internally so can be destroyed
  231. * by the caller. */
  232. void ne_ssl_set_clicert(ne_session *sess, const ne_ssl_client_cert *clicert);
  233. /* Indicate that the certificate 'cert' is trusted; the 'cert' object
  234. * is duplicated internally so can be destroyed by the caller. This
  235. * function has no effect for non-SSL sessions. */
  236. void ne_ssl_trust_cert(ne_session *sess, const ne_ssl_certificate *cert);
  237. /* If the SSL library provided a default set of CA certificates, trust
  238. * this set of CAs. */
  239. void ne_ssl_trust_default_ca(ne_session *sess);
  240. /* Callback used to load a client certificate on demand. If dncount
  241. * is > 0, the 'dnames' array dnames[0] through dnames[dncount-1]
  242. * gives the list of CA names which the server indicated were
  243. * acceptable. The callback should load an appropriate client
  244. * certificate and then pass it to 'ne_ssl_set_clicert'. */
  245. typedef void (*ne_ssl_provide_fn)(void *userdata, ne_session *sess,
  246. const ne_ssl_dname *const *dnames,
  247. int dncount);
  248. /* Register a function to be called when the server requests a client
  249. * certificate. */
  250. void ne_ssl_provide_clicert(ne_session *sess,
  251. ne_ssl_provide_fn fn, void *userdata);
  252. #ifdef WINSCP
  253. const char * ne_ssl_get_version(ne_session *sess);
  254. char * ne_ssl_get_cipher(ne_session *sess);
  255. struct ssl_st;
  256. void ne_init_ssl_session(struct ssl_st *ssl, ne_session *sess);
  257. #endif
  258. /* Set the timeout (in seconds) used when reading from a socket. The
  259. * timeout value must be greater than zero. */
  260. void ne_set_read_timeout(ne_session *sess, int timeout);
  261. /* Set the timeout (in seconds) used when making a connection. The
  262. * timeout value must be greater than zero. */
  263. void ne_set_connect_timeout(ne_session *sess, int timeout);
  264. /* Sets the user-agent string. neon/VERSION will be appended, to make
  265. * the full header "User-Agent: product neon/VERSION".
  266. * If this function is not called, the User-Agent header is not sent.
  267. * The product string must follow the RFC2616 format, i.e.
  268. * product = token ["/" product-version]
  269. * product-version = token
  270. * where token is any alpha-numeric-y string [a-zA-Z0-9]* */
  271. void ne_set_useragent(ne_session *sess, const char *product);
  272. #ifdef WINSCP
  273. void ne_set_realhost(ne_session *sess, const char *realhost);
  274. void ne_ssl_set_certificates_storage(ne_session *sess, const char * filename);
  275. #endif
  276. /* Returns non-zero if next-hop server does not claim compliance to
  277. * HTTP/1.1 or later. */
  278. int ne_version_pre_http11(ne_session *sess);
  279. /* Returns the 'hostport' URI segment for the end-server, e.g.
  280. * "my.server.com:8080". */
  281. const char *ne_get_server_hostport(ne_session *sess);
  282. /* Returns the URL scheme being used for the current session, omitting
  283. * the trailing ':'; e.g. "http" or "https". */
  284. const char *ne_get_scheme(ne_session *sess);
  285. /* Sets the host, scheme, and port fields of the given URI structure
  286. * to that of the configured server and scheme for the session; host
  287. * and scheme are malloc-allocated. No other fields in the URI
  288. * structure are changed. */
  289. void ne_fill_server_uri(ne_session *sess, ne_uri *uri);
  290. /* If a proxy is configured, sets the host and port fields in the
  291. * given URI structure to that of the proxy. If multiple proxies are
  292. * configured, the active is used if any, otherwise the first. The
  293. * hostname is malloc-allocated. No other fields in the URI structure
  294. * are changed; if no proxy is configured or a non-HTTP proxy is in
  295. * use, no fields are changed. */
  296. void ne_fill_proxy_uri(ne_session *sess, ne_uri *uri);
  297. /* Set the error string for the session; takes printf-like format
  298. * string. */
  299. void ne_set_error(ne_session *sess, const char *format, ...)
  300. ne_attribute((format (printf, 2, 3)));
  301. /* Retrieve the error string for the session */
  302. const char *ne_get_error(ne_session *sess);
  303. NE_END_DECLS
  304. #endif /* NE_SESSION_H */