libreport  2.6.3
A tool to inform users about various problems on the running system
libreport_curl.h
1 /*
2  Copyright (C) 2010 ABRT team
3  Copyright (C) 2010 RedHat Inc
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #ifndef LIBREPORT_CURL_H_
20 #define LIBREPORT_CURL_H_
21 
22 #include <curl/curl.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 CURL* xcurl_easy_init();
29 
30 /* Set proxy according to the url and call curl_easy_perform */
31 CURLcode curl_easy_perform_with_proxy(CURL *handle, const char *url);
32 
33 typedef struct post_state {
34  /* Supplied by caller: */
35  int flags;
36  const char *username;
37  const char *password;
38  const char *client_cert_path;
39  const char *client_key_path;
40  const char *cert_authority_cert_path;
41  /* Results of POST transaction: */
42  int http_resp_code;
43  /* cast from CURLcode enum.
44  * 0 = success.
45  * -1 = curl_easy_perform wasn't even reached (file open error, etc).
46  * Else curl_easy_perform's error (which is positive, see curl/curl.h).
47  */
48  int curl_result;
49  unsigned header_cnt;
50  char **headers;
51  char *curl_error_msg;
52  char *body;
53  size_t body_size;
54  char errmsg[CURL_ERROR_SIZE];
55 } post_state_t;
56 
57 post_state_t *new_post_state(int flags);
58 void free_post_state(post_state_t *state);
59 char *find_header_in_post_state(post_state_t *state, const char *str);
60 
61 enum {
62  POST_WANT_HEADERS = (1 << 0),
63  POST_WANT_ERROR_MSG = (1 << 1),
64  POST_WANT_BODY = (1 << 2),
65  POST_WANT_SSL_VERIFY = (1 << 3),
66 };
67 enum {
68  /* Must be -1! CURLOPT_POSTFIELDSIZE interprets -1 as "use strlen" */
69  POST_DATA_STRING = -1,
70  POST_DATA_FROMFILE = -2,
71  POST_DATA_FROMFILE_PUT = -3,
72  POST_DATA_FROMFILE_AS_FORM_DATA = -4,
73  POST_DATA_STRING_AS_FORM_DATA = -5,
74  POST_DATA_GET = -6,
75 };
76 int
77 post(post_state_t *state,
78  const char *url,
79  const char *content_type,
80  const char **additional_headers,
81  const char *data,
82  off_t data_size);
83 static inline int
84 get(post_state_t *state,
85  const char *url,
86  const char *content_type,
87  const char **additional_headers)
88 {
89  return post(state, url, content_type, additional_headers,
90  NULL, POST_DATA_GET);
91 }
92 static inline int
93 post_string(post_state_t *state,
94  const char *url,
95  const char *content_type,
96  const char **additional_headers,
97  const char *str)
98 {
99  return post(state, url, content_type, additional_headers,
100  str, POST_DATA_STRING);
101 }
102 static inline int
103 post_string_as_form_data(post_state_t *state,
104  const char *url,
105  const char *content_type,
106  const char **additional_headers,
107  const char *str)
108 {
109  return post(state, url, content_type, additional_headers,
110  str, POST_DATA_STRING_AS_FORM_DATA);
111 }
112 static inline int
113 post_file(post_state_t *state,
114  const char *url,
115  const char *content_type,
116  const char **additional_headers,
117  const char *filename)
118 {
119  return post(state, url, content_type, additional_headers,
120  filename, POST_DATA_FROMFILE);
121 }
122 static inline int
123 post_file_as_form(post_state_t *state,
124  const char *url,
125  const char *content_type,
126  const char **additional_headers,
127  const char *filename)
128 {
129  return post(state, url, content_type, additional_headers,
130  filename, POST_DATA_FROMFILE_AS_FORM_DATA);
131 }
132 
133 enum {
134  UPLOAD_FILE_NOFLAGS = 0,
135  UPLOAD_FILE_HANDLE_ACCESS_DENIALS = 1 << 0,
136 };
137 
138 #define upload_file libreport_upload_file
139 char *upload_file(const char *url, const char *filename);
140 
141 #define upload_file_ext libreport_upload_file_ext
142 char *upload_file_ext(post_state_t *post_state,
143  const char *url,
144  const char *filename,
145  int flags);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif