Imported Upstream version 2.2.1
[platform/upstream/gpg2.git] / dirmngr / http.h
1 /* http.h  -  HTTP protocol handler
2  * Copyright (C) 1999, 2000, 2001, 2003, 2006,
3  *               2010 Free Software Foundation, Inc.
4  * Copyright (C) 2015  g10 Code GmbH
5  *
6  * This file is part of GnuPG.
7  *
8  * This file is free software; you can redistribute it and/or modify
9  * it under the terms of either
10  *
11  *   - the GNU Lesser General Public License as published by the Free
12  *     Software Foundation; either version 3 of the License, or (at
13  *     your option) any later version.
14  *
15  * or
16  *
17  *   - the GNU General Public License as published by the Free
18  *     Software Foundation; either version 2 of the License, or (at
19  *     your option) any later version.
20  *
21  * or both in parallel, as here.
22  *
23  * This file is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, see <https://www.gnu.org/licenses/>.
30  */
31 #ifndef GNUPG_COMMON_HTTP_H
32 #define GNUPG_COMMON_HTTP_H
33
34 #include <gpg-error.h>
35
36 struct uri_tuple_s
37 {
38   struct uri_tuple_s *next;
39   const char *name;     /* A pointer into name. */
40   char  *value;         /* A pointer to value (a Nul is always appended). */
41   size_t valuelen;      /* The real length of the value; we need it
42                            because the value may contain embedded Nuls. */
43   int no_value;         /* True if no value has been given in the URL. */
44 };
45 typedef struct uri_tuple_s *uri_tuple_t;
46
47 struct parsed_uri_s
48 {
49   /* All these pointers point into BUFFER; most stuff is not escaped. */
50   char *original;       /* Unmodified copy of the parsed URI.  */
51   char *scheme;         /* Pointer to the scheme string (always lowercase). */
52   unsigned int is_http:1; /* This is a HTTP style URI.   */
53   unsigned int use_tls:1; /* Whether TLS should be used. */
54   unsigned int opaque:1;/* Unknown scheme; PATH has the rest.  */
55   unsigned int v6lit:1; /* Host was given as a literal v6 address.  */
56   unsigned int onion:1; /* .onion address given.  */
57   unsigned int explicit_port :1; /* The port was explicitly specified.  */
58   char *auth;           /* username/password for basic auth.  */
59   char *host;           /* Host (converted to lowercase). */
60   unsigned short port;  /* Port (always set if the host is set). */
61   char *path;           /* Path. */
62   uri_tuple_t params;   /* ";xxxxx" */
63   uri_tuple_t query;    /* "?xxx=yyy" */
64   char buffer[1];       /* Buffer which holds a (modified) copy of the URI. */
65 };
66 typedef struct parsed_uri_s *parsed_uri_t;
67
68 struct uri_tuple_s *uri_query_lookup (parsed_uri_t uri, const char *key);
69
70 typedef enum
71   {
72     HTTP_REQ_GET  = 1,
73     HTTP_REQ_HEAD = 2,
74     HTTP_REQ_POST = 3,
75     HTTP_REQ_OPAQUE = 4  /* Internal use.  */
76   }
77 http_req_t;
78
79 /* We put the flag values into an enum, so that gdb can display them. */
80 enum
81   {
82     HTTP_FLAG_TRY_PROXY = 1,     /* Try to use a proxy.  */
83     HTTP_FLAG_SHUTDOWN = 2,      /* Close sending end after the request.  */
84     HTTP_FLAG_FORCE_TOR = 4,     /* Force a TOR connection.  */
85     HTTP_FLAG_LOG_RESP = 8,      /* Log the server response.  */
86     HTTP_FLAG_FORCE_TLS = 16,    /* Force the use of TLS.  */
87     HTTP_FLAG_IGNORE_CL = 32,    /* Ignore content-length.  */
88     HTTP_FLAG_IGNORE_IPv4 = 64,  /* Do not use IPv4.  */
89     HTTP_FLAG_IGNORE_IPv6 = 128, /* Do not use IPv6.  */
90     HTTP_FLAG_TRUST_DEF   = 256, /* Use the CAs configured for HKP.  */
91     HTTP_FLAG_TRUST_SYS   = 512, /* Also use the system defined CAs.  */
92     HTTP_FLAG_NO_CRL     = 1024  /* Do not consult CRLs for https.  */
93   };
94
95
96 struct http_session_s;
97 typedef struct http_session_s *http_session_t;
98
99 struct http_context_s;
100 typedef struct http_context_s *http_t;
101
102 /* A TLS verify callback function.  */
103 typedef gpg_error_t (*http_verify_cb_t) (void *opaque,
104                                          http_t http,
105                                          http_session_t session,
106                                          unsigned int flags,
107                                          void *tls_context);
108
109 void http_set_verbose (int verbose, int debug);
110
111 void http_register_tls_callback (gpg_error_t (*cb)(http_t,http_session_t,int));
112 void http_register_tls_ca (const char *fname);
113 void http_register_netactivity_cb (void (*cb)(void));
114
115
116 gpg_error_t http_session_new (http_session_t *r_session,
117                               const char *intended_hostname,
118                               unsigned int flags,
119                               http_verify_cb_t cb,
120                               void *cb_value);
121 http_session_t http_session_ref (http_session_t sess);
122 void http_session_release (http_session_t sess);
123
124 void http_session_set_log_cb (http_session_t sess,
125                               void (*cb)(http_session_t, gpg_error_t,
126                                          const char *,
127                                          const void **, size_t *));
128 void http_session_set_timeout (http_session_t sess, unsigned int timeout);
129
130
131 gpg_error_t http_parse_uri (parsed_uri_t *ret_uri, const char *uri,
132                             int no_scheme_check);
133
134 void http_release_parsed_uri (parsed_uri_t uri);
135
136 gpg_error_t http_raw_connect (http_t *r_hd,
137                               const char *server, unsigned short port,
138                               unsigned int flags, const char *srvtag,
139                               unsigned int timeout);
140
141 gpg_error_t http_open (http_t *r_hd, http_req_t reqtype,
142                        const char *url,
143                        const char *httphost,
144                        const char *auth,
145                        unsigned int flags,
146                        const char *proxy,
147                        http_session_t session,
148                        const char *srvtag,
149                        strlist_t headers);
150
151 void http_start_data (http_t hd);
152
153 gpg_error_t http_wait_response (http_t hd);
154
155 void http_close (http_t hd, int keep_read_stream);
156
157 gpg_error_t http_open_document (http_t *r_hd,
158                                 const char *document,
159                                 const char *auth,
160                                 unsigned int flags,
161                                 const char *proxy,
162                                 http_session_t session,
163                                 const char *srvtag,
164                                 strlist_t headers);
165
166 estream_t http_get_read_ptr (http_t hd);
167 estream_t http_get_write_ptr (http_t hd);
168 unsigned int http_get_status_code (http_t hd);
169 const char *http_get_tls_info (http_t hd, const char *what);
170 const char *http_get_header (http_t hd, const char *name);
171 const char **http_get_header_names (http_t hd);
172 gpg_error_t http_verify_server_credentials (http_session_t sess);
173
174 char *http_escape_string (const char *string, const char *specials);
175 char *http_escape_data (const void *data, size_t datalen, const char *specials);
176
177
178 #endif /*GNUPG_COMMON_HTTP_H*/