-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.c
204 lines (159 loc) · 6.07 KB
/
http.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* HTTP notifier for CUPS.
*
* Copyright 2017 by Aiwip Ltd.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more information.
*/
/*
* Include necessary headers...
*/
#define _IPP_PRIVATE_STRUCTURES 1
#include <curl/curl.h>
#include <cups/cups.h>
#include <json-c/json.h>
#include <signal.h>
#include <stdio.h>
/*
* Local functions...
*/
/*
* 'generate_request_body()' - Generate request body for webhook...
*/
char *generate_request_body(ipp_t *ipp, //* I - IPP request *
char *body) //* B - Body of webhook request *
{
cups_lang_t *lang; /* Language info */
ipp_tag_t group; /* Current group */
ipp_attribute_t *attr; /* Current attribute */
char buffer[1024]; /* Value buffer */
json_object *request_json = json_object_new_object(); /* Root JSON object */
json_object *group_json = request_json; /* Current JSON object for attributes */
json_object *json_value; /* Current JSON object for response */
if ((lang = cupsLangDefault()) == NULL)
exit(1);
json_object *subject_value = json_object_new_string(cupsNotifySubject(lang, ipp));
json_object_object_add(request_json, "subject", subject_value);
json_object *message_value = json_object_new_string(cupsNotifyText(lang, ipp));
json_object_object_add(request_json, "message", message_value);
for (group = IPP_TAG_ZERO, attr = ipp->attrs; attr; attr = attr->next)
{
if (attr->group_tag == IPP_TAG_ZERO || !attr->name)
{
group = IPP_TAG_ZERO;
continue;
}
if (group != attr->group_tag)
{
group = attr->group_tag;
group_json = json_object_new_object();
json_object_object_add(request_json, ippTagString(group), group_json);
}
// Integer, Boolean or Enum
if (attr->value_tag == IPP_TAG_INTEGER ||
attr->value_tag == IPP_TAG_BOOLEAN ||
attr->value_tag == IPP_TAG_ENUM )
{
json_value = json_object_new_int(ippGetInteger(attr, 0));
}
// All Other Values
else
{
ippAttributeString(attr, buffer, sizeof(buffer));
json_value = json_object_new_string(buffer);
}
json_object_object_add(group_json, (char *)attr->name, json_value);
}
strcpy(body, json_object_to_json_string(request_json));
}
/*
* 'main()' - Read events and send HTTP notifications.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line arguments */
char *argv[]) /* I - Command-line arguments */
{
int i; /* Looping var */
char scheme[32], /* URI scheme ("http") */
host[1024], /* Hostname for remote HTTP */
username[256], /* Username for remote HTTP */
resource[1024]; /* HTTP endpoint */
int port; /* Port number for remote HTTP */
struct sigaction action; /* POSIX sigaction data */
ipp_t *event; /* Event from scheduler */
ipp_state_t state; /* IPP event state */
CURL *curl; /* CURL HTTP Client */
CURLcode res; /* HTTP Response */
struct curl_slist *headers=NULL; /* HTTP Headers */
char baseurl[1024]; /* Base URL */
char request_body[40000]; /* Body for webhook request */
/*
* Don't buffer stderr...
*/
setbuf(stderr, NULL);
/*
* Ignore SIGPIPE signals...
*/
memset(&action, 0, sizeof(action));
action.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &action, NULL);
fprintf(stderr, "DEBUG: argc=%d\n", argc);
for (i = 0; i < argc; i ++)
fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
fprintf(stderr, "DEBUG: TMPDIR=\"%s\"\n", getenv("TMPDIR"));
/*
* See whether the HTTP Endpoint is based locally or remotely...
*/
if (httpSeparateURI(HTTP_URI_CODING_ALL, argv[1], scheme, sizeof(scheme),
username, sizeof(username), host, sizeof(host), &port,
resource, sizeof(resource)) < HTTP_URI_OK)
{
fprintf(stderr, "ERROR: Bad HTTP URI \"%s\"!\n", argv[1]);
return (1);
}
curl = curl_easy_init();
httpAssembleURI(HTTP_URI_CODING_ALL, baseurl, sizeof(baseurl), scheme, NULL, host, port, resource);
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, baseurl);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/*
* Loop forever until we run out of events...
*/
for (;;)
{
/*
* Read the next event...
*/
event = ippNew();
while ((state = ippReadFile(0, event)) != IPP_DATA)
{
if (state <= IPP_IDLE)
break;
}
fprintf(stderr, "DEBUG: state=%d\n", state);
if (state == IPP_ERROR)
fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr);
if (state <= IPP_IDLE)
break;
generate_request_body(event, request_body);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_body);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
exit(0);
/*
* Free the memory used for this event...
*/
if (request_body)
free(request_body);
if (curl)
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
ippDelete(event);
event = NULL;
}
return (0);
}