]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-support.c
4 * HTTP support routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * httpSeparate() - Separate a Universal Resource Identifier into its
30 * httpSeparate2() - Separate a Universal Resource Identifier into its
32 * httpStatus() - Return a short string describing a HTTP status code.
33 * cups_hstrerror() - hstrerror() emulation function for Solaris and others...
34 * http_copy_decode() - Copy and decode a URI.
38 * Include necessary headers...
55 static const char *http_copy_decode(char *dst
, const char *src
,
56 int dstsize
, const char *term
);
60 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
65 httpSeparate(const char *uri
, /* I - Universal Resource Identifier */
66 char *method
, /* O - Method [32] (http, https, etc.) */
67 char *username
, /* O - Username [1024] */
68 char *host
, /* O - Hostname [1024] */
69 int *port
, /* O - Port number to use */
70 char *resource
) /* O - Resource/filename [1024] */
72 httpSeparate2(uri
, method
, 32, username
, HTTP_MAX_URI
, host
, HTTP_MAX_URI
,
73 port
, resource
, HTTP_MAX_URI
);
78 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
83 httpSeparate2(const char *uri
, /* I - Universal Resource Identifier */
84 char *method
, /* O - Method (http, https, etc.) */
85 int methodlen
, /* I - Size of method buffer */
86 char *username
, /* O - Username */
87 int usernamelen
, /* I - Size of username buffer */
88 char *host
, /* O - Hostname */
89 int hostlen
, /* I - Size of hostname buffer */
90 int *port
, /* O - Port number to use */
91 char *resource
, /* O - Resource/filename */
92 int resourcelen
) /* I - Size of resource buffer */
94 char *ptr
; /* Pointer into string... */
95 const char *atsign
, /* @ sign */
96 *slash
; /* Separator */
100 * Range check input...
103 if (uri
== NULL
|| method
== NULL
|| username
== NULL
|| host
== NULL
||
104 port
== NULL
|| resource
== NULL
)
108 * Grab the method portion of the URI...
111 if (strncmp(uri
, "//", 2) == 0)
114 * Workaround for HP IPP client bug...
117 strlcpy(method
, "ipp", methodlen
);
122 * Standard URI with method...
125 uri
= http_copy_decode(host
, uri
, hostlen
, ":");
131 * If the method contains a period or slash, then it's probably
132 * hostname/filename...
135 if (strchr(host
, '.') != NULL
|| strchr(host
, '/') != NULL
|| *uri
== '\0')
137 if ((ptr
= strchr(host
, '/')) != NULL
)
139 strlcpy(resource
, ptr
, resourcelen
);
145 if (isdigit(*uri
& 255))
148 * OK, we have "hostname:port[/resource]"...
151 *port
= strtol(uri
, (char **)&uri
, 10);
154 strlcpy(resource
, uri
, resourcelen
);
159 strlcpy(method
, "http", methodlen
);
164 strlcpy(method
, host
, methodlen
);
168 * If the method starts with less than 2 slashes then it is a local resource...
171 if (strncmp(uri
, "//", 2) != 0)
173 strlcpy(resource
, uri
, resourcelen
);
182 * Grab the username, if any...
187 if ((slash
= strchr(uri
, '/')) == NULL
)
188 slash
= uri
+ strlen(uri
);
190 if ((atsign
= strchr(uri
, '@')) != NULL
&& atsign
< slash
)
193 * Got a username:password combo...
196 uri
= http_copy_decode(username
, uri
, usernamelen
, "@") + 1;
202 * Grab the hostname...
205 uri
= http_copy_decode(host
, uri
, hostlen
, ":/");
209 if (strcasecmp(method
, "http") == 0)
211 else if (strcasecmp(method
, "https") == 0)
213 else if (strcasecmp(method
, "ipp") == 0)
215 else if (strcasecmp(method
, "lpd") == 0)
217 else if (strcasecmp(method
, "socket") == 0) /* Not registered yet... */
225 * Parse port number...
228 *port
= strtol(uri
+ 1, (char **)&uri
, 10);
234 * Hostname but no port or path...
243 * The remaining portion is the resource string...
246 http_copy_decode(resource
, uri
, resourcelen
, "");
251 * 'httpStatus()' - Return a short string describing a HTTP status code.
254 const char * /* O - String or NULL */
255 httpStatus(http_status_t status
) /* I - HTTP status code */
261 case HTTP_SWITCHING_PROTOCOLS
:
262 return ("Switching Protocols");
269 case HTTP_NO_CONTENT
:
270 return ("No Content");
271 case HTTP_NOT_MODIFIED
:
272 return ("Not Modified");
273 case HTTP_BAD_REQUEST
:
274 return ("Bad Request");
275 case HTTP_UNAUTHORIZED
:
276 return ("Unauthorized");
277 case HTTP_FORBIDDEN
:
278 return ("Forbidden");
279 case HTTP_NOT_FOUND
:
280 return ("Not Found");
281 case HTTP_REQUEST_TOO_LARGE
:
282 return ("Request Entity Too Large");
283 case HTTP_URI_TOO_LONG
:
284 return ("URI Too Long");
285 case HTTP_UPGRADE_REQUIRED
:
286 return ("Upgrade Required");
287 case HTTP_NOT_IMPLEMENTED
:
288 return ("Not Implemented");
289 case HTTP_NOT_SUPPORTED
:
290 return ("Not Supported");
297 #ifndef HAVE_HSTRERROR
299 * 'cups_hstrerror()' - hstrerror() emulation function for Solaris and others...
302 const char * /* O - Error string */
303 cups_hstrerror(int error
) /* I - Error number */
305 static const char * const errors
[] = /* Error strings */
310 "Unrecoverable lookup error.",
311 "No data associated with name."
315 if (error
< 0 || error
> 4)
316 return ("Unknown hostname lookup error.");
318 return (errors
[error
]);
320 #endif /* !HAVE_HSTRERROR */
324 * 'http_copy_decode()' - Copy and decode a URI.
327 static const char * /* O - New source pointer */
328 http_copy_decode(char *dst
, /* O - Destination buffer */
329 const char *src
, /* I - Source pointer */
330 int dstsize
, /* I - Destination size */
331 const char *term
) /* I - Terminating characters */
333 char *ptr
, /* Pointer into buffer */
334 *end
; /* End of buffer */
335 int quoted
; /* Quoted character */
339 * Copy the src to the destination until we hit a terminating character
340 * or the end of the string.
343 for (ptr
= dst
, end
= dst
+ dstsize
- 1; *src
&& !strchr(term
, *src
); src
++)
346 if (*src
== '%' && isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
349 * Grab a hex-encoded character...
354 quoted
= (tolower(*src
) - 'a' + 10) << 4;
356 quoted
= (*src
- '0') << 4;
360 quoted
|= tolower(*src
) - 'a' + 10;
362 quoted
|= *src
- '0';