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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/*
* "$Id$"
*
* IPP utilities for the Common UNIX Printing System (CUPS).
*
* Copyright 1997-2006 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* property of Easy Software Products and are protected by Federal
* copyright law. Distribution and use rights are outlined in the file
* "LICENSE.txt" which should have been included with this file. If this
* file is missing or damaged please contact Easy Software Products
* at:
*
* Attn: CUPS Licensing Information
* Easy Software Products
* 44141 Airport View Drive, Suite 204
* Hollywood, Maryland 20636 USA
*
* Voice: (301) 373-9600
* EMail: cups-info@cups.org
* WWW: http://www.cups.org
*
* This file is subject to the Apple OS-Developed Software exception.
*
* Contents:
*
* cupsDoFileRequest() - Do an IPP request with a file.
* cupsDoRequest() - Do an IPP request.
* _cupsSetError() - Set the last IPP status code and status-message.
*/
/*
* Include necessary headers...
*/
#include "globals.h"
#include "debug.h"
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#if defined(WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
#endif /* WIN32 || __EMX__ */
/*
* 'cupsDoFileRequest()' - Do an IPP request with a file.
*
* This function sends the IPP request to the specified server, retrying
* and authenticating as necessary. The request is freed with ippDelete()
* after receiving a valid IPP response.
*/
ipp_t * /* O - Response data */
cupsDoFileRequest(http_t *http, /* I - HTTP connection to server */
ipp_t *request, /* I - IPP request */
const char *resource, /* I - HTTP resource for POST */
const char *filename) /* I - File to send or NULL for none */
{
ipp_t *response; /* IPP response data */
size_t length; /* Content-Length value */
http_status_t status; /* Status of HTTP request */
int got_status; /* Did we get the status? */
ipp_state_t state; /* State of IPP processing */
FILE *file; /* File to send */
struct stat fileinfo; /* File information */
int bytes; /* Number of bytes read/written */
char buffer[32768]; /* Output buffer */
http_status_t expect; /* Expect: header to use */
DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
http, request, resource ? resource : "(null)",
filename ? filename : "(null)"));
if (http == NULL || request == NULL || resource == NULL)
{
if (request != NULL)
ippDelete(request);
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (NULL);
}
/*
* See if we have a file to send...
*/
if (filename != NULL)
{
if (stat(filename, &fileinfo))
{
/*
* Can't get file information!
*/
_cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
strerror(errno));
ippDelete(request);
return (NULL);
}
#ifdef WIN32
if (fileinfo.st_mode & _S_IFDIR)
#else
if (S_ISDIR(fileinfo.st_mode))
#endif /* WIN32 */
{
/*
* Can't send a directory...
*/
ippDelete(request);
_cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR));
return (NULL);
}
if ((file = fopen(filename, "rb")) == NULL)
{
/*
* Can't open file!
*/
_cupsSetError(errno == ENOENT ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
strerror(errno));
ippDelete(request);
return (NULL);
}
}
else
file = NULL;
/*
* Loop until we can send the request without authorization problems.
*/
response = NULL;
status = HTTP_ERROR;
expect = HTTP_CONTINUE;
while (response == NULL)
{
DEBUG_puts("cupsDoFileRequest: setup...");
/*
* Setup the HTTP variables needed...
*/
length = ippLength(request);
if (filename)
length += fileinfo.st_size;
httpClearFields(http);
httpSetLength(http, length);
httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
httpSetExpect(http, expect);
DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http->authstring));
/*
* Try the request...
*/
DEBUG_puts("cupsDoFileRequest: post...");
if (httpPost(http, resource))
{
if (httpReconnect(http))
{
status = HTTP_ERROR;
break;
}
else
continue;
}
/*
* Send the IPP data...
*/
DEBUG_puts("cupsDoFileRequest: ipp write...");
request->state = IPP_IDLE;
status = HTTP_CONTINUE;
got_status = 0;
while ((state = ippWrite(http, request)) != IPP_DATA)
if (state == IPP_ERROR)
break;
else if (httpCheck(http))
{
got_status = 1;
if ((status = httpUpdate(http)) != HTTP_CONTINUE)
break;
}
if (!got_status)
{
/*
* Wait up to 1 second to get the 100-continue response...
*/
if (httpWait(http, 1000))
status = httpUpdate(http);
}
else if (httpCheck(http))
status = httpUpdate(http);
if (status == HTTP_CONTINUE && state == IPP_DATA && filename)
{
DEBUG_puts("cupsDoFileRequest: file write...");
/*
* Send the file...
*/
rewind(file);
while ((bytes = (int)fread(buffer, 1, sizeof(buffer), file)) > 0)
{
if (httpCheck(http))
{
if ((status = httpUpdate(http)) != HTTP_CONTINUE)
break;
}
if (httpWrite2(http, buffer, bytes) < bytes)
break;
}
}
/*
* Get the server's return status...
*/
DEBUG_puts("cupsDoFileRequest: update...");
while (status == HTTP_CONTINUE)
status = httpUpdate(http);
DEBUG_printf(("cupsDoFileRequest: status = %d\n", status));
if (status == HTTP_UNAUTHORIZED)
{
DEBUG_puts("cupsDoFileRequest: unauthorized...");
/*
* Flush any error message...
*/
httpFlush(http);
/*
* See if we can do authentication...
*/
if (cupsDoAuthentication(http, "POST", resource))
break;
if (httpReconnect(http))
{
status = HTTP_ERROR;
break;
}
continue;
}
else if (status == HTTP_ERROR)
{
DEBUG_printf(("cupsDoFileRequest: http->error=%d (%s)\n", http->error,
strerror(http->error)));
#ifdef WIN32
if (http->error != WSAENETDOWN && http->error != WSAENETUNREACH &&
http->error != ETIMEDOUT)
#else
if (http->error != ENETDOWN && http->error != ENETUNREACH &&
http->error != ETIMEDOUT)
#endif /* WIN32 */
continue;
else
break;
}
#ifdef HAVE_SSL
else if (status == HTTP_UPGRADE_REQUIRED)
{
/* Flush any error message... */
httpFlush(http);
/* Reconnect... */
if (httpReconnect(http))
{
status = HTTP_ERROR;
break;
}
/* Upgrade with encryption... */
httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
/* Try again, this time with encryption enabled... */
continue;
}
#endif /* HAVE_SSL */
else if (status == HTTP_EXPECTATION_FAILED)
{
/*
* Don't try using the Expect: header the next time around...
*/
expect = (http_status_t)0;
}
else if (status != HTTP_OK)
{
DEBUG_printf(("cupsDoFileRequest: error %d...\n", status));
/*
* Flush any error message...
*/
httpFlush(http);
break;
}
else
{
/*
* Read the response...
*/
DEBUG_puts("cupsDoFileRequest: response...");
response = ippNew();
while ((state = ippRead(http, response)) != IPP_DATA)
if (state == IPP_ERROR)
{
/*
* Delete the response...
*/
DEBUG_puts("IPP read error!");
ippDelete(response);
response = NULL;
_cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno));
break;
}
}
}
/*
* Close the file if needed...
*/
if (filename != NULL)
fclose(file);
/*
* Flush any remaining data...
*/
httpFlush(http);
/*
* Delete the original request and return the response...
*/
ippDelete(request);
if (response)
{
ipp_attribute_t *attr; /* status-message attribute */
attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
_cupsSetError(response->request.status.status_code,
attr ? attr->values[0].string.text :
ippErrorString(response->request.status.status_code));
}
else if (status != HTTP_OK)
{
switch (status)
{
case HTTP_NOT_FOUND :
_cupsSetError(IPP_NOT_FOUND, httpStatus(status));
break;
case HTTP_UNAUTHORIZED :
_cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status));
break;
case HTTP_FORBIDDEN :
_cupsSetError(IPP_FORBIDDEN, httpStatus(status));
break;
case HTTP_BAD_REQUEST :
_cupsSetError(IPP_BAD_REQUEST, httpStatus(status));
break;
case HTTP_REQUEST_TOO_LARGE :
_cupsSetError(IPP_REQUEST_VALUE, httpStatus(status));
break;
case HTTP_NOT_IMPLEMENTED :
_cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status));
break;
case HTTP_NOT_SUPPORTED :
_cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status));
break;
default :
DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
status));
_cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status));
break;
}
}
return (response);
}
/*
* 'cupsDoRequest()' - Do an IPP request.
*
* This function sends the IPP request to the specified server, retrying
* and authenticating as necessary. The request is freed with ippDelete()
* after receiving a valid IPP response.
*/
ipp_t * /* O - Response data */
cupsDoRequest(http_t *http, /* I - HTTP connection to server */
ipp_t *request, /* I - IPP request */
const char *resource) /* I - HTTP resource for POST */
{
return (cupsDoFileRequest(http, request, resource, NULL));
}
/*
* '_cupsSetError()' - Set the last IPP status code and status-message.
*/
void
_cupsSetError(ipp_status_t status, /* I - IPP status code */
const char *message) /* I - status-message value */
{
_cups_globals_t *cg; /* Global data */
cg = _cupsGlobals();
cg->last_error = status;
if (cg->last_status_message)
{
free(cg->last_status_message);
cg->last_status_message = NULL;
}
if (message)
cg->last_status_message = strdup(message);
}
/*
* End of "$Id$".
*/
|