Revision 75cef49f
Added query_add_event to mainbox code
mainbox/query.c | ||
---|---|---|
1 | 1 |
#include "query.h" |
2 |
#include "event.h" |
|
2 | 3 |
#include <stdio.h> |
3 | 4 |
#include <curl/curl.h> |
4 | 5 |
|
... | ... | |
20 | 21 |
curl_global_cleanup(); |
21 | 22 |
} |
22 | 23 |
|
23 |
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
|
|
24 |
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) {
|
|
24 | 25 |
int *resultp = userp; |
25 | 26 |
char *str = buffer; |
26 | 27 |
|
... | ... | |
32 | 33 |
return nmemb; |
33 | 34 |
} |
34 | 35 |
|
35 |
int query_user_permission(int tool_id, int user_id) { |
|
36 |
/* |
|
37 |
* query_user_permission |
|
38 |
* |
|
39 |
* Makes an HTTP request to the CRM server to see if user_id has access to |
|
40 |
* tool_id. Returns 1 if the server replies with '1' or 0 otherwise. |
|
41 |
*/ |
|
42 |
int query_user_permission(int tool_id, unsigned int user_id) { |
|
36 | 43 |
CURL* handle; |
37 | 44 |
CURLcode error_code; |
38 | 45 |
char url[1024]; |
... | ... | |
47 | 54 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, url); |
48 | 55 |
if (error_code) goto error; |
49 | 56 |
|
50 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
|
|
57 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool);
|
|
51 | 58 |
if (error_code) goto error; |
52 | 59 |
|
53 | 60 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result); |
... | ... | |
73 | 80 |
curl_easy_cleanup(handle); |
74 | 81 |
return 0; |
75 | 82 |
} |
83 |
|
|
84 |
/* |
|
85 |
* query_add_event |
|
86 |
* |
|
87 |
* Makes an HTTPS POST request to add an event to the CRM server, including |
|
88 |
* user, tool, start time, and stop time. Reads the password from password.txt. |
|
89 |
* Returns 0 if successful, or 1 if there was an error and the caller should |
|
90 |
* try the same event again later. |
|
91 |
* |
|
92 |
* Times are represented as strftime's "%F %T", which is like "YYYY-MM-DD |
|
93 |
* HH:MM:SS" with 24-hour time |
|
94 |
*/ |
|
95 |
int query_add_event(struct event_t *event) { |
|
96 |
CURL* handle; |
|
97 |
CURLcode error_code; |
|
98 |
struct curl_httppost *formpost = NULL, *lastptr = NULL; |
|
99 |
char buf[1024]; |
|
100 |
struct tm *timeinfo; |
|
101 |
long response = 0; |
|
102 |
|
|
103 |
handle = curl_easy_init(); |
|
104 |
if (handle == NULL) |
|
105 |
return 1; |
|
106 |
|
|
107 |
curl_formadd(&formpost, &lastptr, |
|
108 |
CURLFORM_COPYNAME, "type", |
|
109 |
CURLFORM_COPYCONTENTS, "usage", |
|
110 |
CURLFORM_END); |
|
111 |
|
|
112 |
timeinfo = localtime(&event->tstart); |
|
113 |
strftime(buf, sizeof(buf), "%F %T", timeinfo); |
|
114 |
curl_formadd(&formpost, &lastptr, |
|
115 |
CURLFORM_COPYNAME, "tstart", |
|
116 |
CURLFORM_COPYCONTENTS, buf, |
|
117 |
CURLFORM_END); |
|
118 |
|
|
119 |
timeinfo = localtime(&event->tend); |
|
120 |
strftime(buf, sizeof(buf), "%F %T", timeinfo); |
|
121 |
curl_formadd(&formpost, &lastptr, |
|
122 |
CURLFORM_COPYNAME, "tend", |
|
123 |
CURLFORM_COPYCONTENTS, buf, |
|
124 |
CURLFORM_END); |
|
125 |
|
|
126 |
sprintf(buf, "%08x", event->user); |
|
127 |
curl_formadd(&formpost, &lastptr, |
|
128 |
CURLFORM_COPYNAME, "user", |
|
129 |
CURLFORM_COPYCONTENTS, buf, |
|
130 |
CURLFORM_END); |
|
131 |
|
|
132 |
sprintf(buf, "%d", event->tool_id); |
|
133 |
curl_formadd(&formpost, &lastptr, |
|
134 |
CURLFORM_COPYNAME, "machine", |
|
135 |
CURLFORM_COPYCONTENTS, buf, |
|
136 |
CURLFORM_END); |
|
137 |
|
|
138 |
sprintf(buf, "https://%s/add_event/", server); |
|
139 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, buf); |
|
140 |
if (error_code) goto error; |
|
141 |
|
|
142 |
error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost); |
|
143 |
if (error_code) goto error; |
|
144 |
|
|
145 |
error_code = curl_easy_perform(handle); |
|
146 |
if (error_code) goto error; |
|
147 |
|
|
148 |
error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response); |
|
149 |
if (error_code) goto error; |
|
150 |
if (response >= 400) |
|
151 |
fprintf(stderr, "Error %ld from %s\n", response, buf); |
|
152 |
else if (response > 200) |
|
153 |
fprintf(stderr, "Warning: response %ld from %s\n", response, buf); |
|
154 |
|
|
155 |
curl_easy_cleanup(handle); |
|
156 |
return response >= 300; |
|
157 |
|
|
158 |
error: |
|
159 |
fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code)); |
|
160 |
curl_easy_cleanup(handle); |
|
161 |
return 1; |
|
162 |
} |
Also available in: Unified diff