libreport  2.2.2
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  /* Results of POST transaction: */
41  int http_resp_code;
42  /* cast from CURLcode enum.
43  * 0 = success.
44  * -1 = curl_easy_perform wasn't even reached (file open error, etc).
45  * Else curl_easy_perform's error (which is positive, see curl/curl.h).
46  */
47  int curl_result;
48  unsigned header_cnt;
49  char **headers;
50  char *curl_error_msg;
51  char *body;
52  size_t body_size;
53  char errmsg[CURL_ERROR_SIZE];
54 } post_state_t;
55 
56 post_state_t *new_post_state(int flags);
57 void free_post_state(post_state_t *state);
58 char *find_header_in_post_state(post_state_t *state, const char *str);
59 
60 enum {
61  POST_WANT_HEADERS = (1 << 0),
62  POST_WANT_ERROR_MSG = (1 << 1),
63  POST_WANT_BODY = (1 << 2),
64  POST_WANT_SSL_VERIFY = (1 << 3),
65 };
66 enum {
67  /* Must be -1! CURLOPT_POSTFIELDSIZE interprets -1 as "use strlen" */
68  POST_DATA_STRING = -1,
69  POST_DATA_FROMFILE = -2,
70  POST_DATA_FROMFILE_PUT = -3,
71  POST_DATA_FROMFILE_AS_FORM_DATA = -4,
72  POST_DATA_STRING_AS_FORM_DATA = -5,
73 };
74 int
75 post(post_state_t *state,
76  const char *url,
77  const char *content_type,
78  const char **additional_headers,
79  const char *data,
80  off_t data_size);
81 static inline int
82 post_string(post_state_t *state,
83  const char *url,
84  const char *content_type,
85  const char **additional_headers,
86  const char *str)
87 {
88  return post(state, url, content_type, additional_headers,
89  str, POST_DATA_STRING);
90 }
91 static inline int
92 post_string_as_form_data(post_state_t *state,
93  const char *url,
94  const char *content_type,
95  const char **additional_headers,
96  const char *str)
97 {
98  return post(state, url, content_type, additional_headers,
99  str, POST_DATA_STRING_AS_FORM_DATA);
100 }
101 static inline int
102 post_file(post_state_t *state,
103  const char *url,
104  const char *content_type,
105  const char **additional_headers,
106  const char *filename)
107 {
108  return post(state, url, content_type, additional_headers,
109  filename, POST_DATA_FROMFILE);
110 }
111 static inline int
112 post_file_as_form(post_state_t *state,
113  const char *url,
114  const char *content_type,
115  const char **additional_headers,
116  const char *filename)
117 {
118  return post(state, url, content_type, additional_headers,
119  filename, POST_DATA_FROMFILE_AS_FORM_DATA);
120 }
121 
122 #define upload_file libreport_upload_file
123 char *upload_file(const char *url, const char *filename);
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif