#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include "string.h"
#include "http.h"
#include "ipp.h"
void
httpSeparate(const char *uri,
char *method,
char *username,
char *host,
int *port,
char *resource)
{
char *ptr;
const char *atsign,
*slash;
char safeuri[HTTP_MAX_URI];
char quoted;
if (uri == NULL || method == NULL || username == NULL || host == NULL ||
port == NULL || resource == NULL)
return;
strlcpy(safeuri, uri, sizeof(safeuri));
uri = safeuri;
if (strncmp(uri, "//", 2) == 0)
{
strcpy(method, "ipp");
}
else
{
for (ptr = host; *uri != ':' && *uri != '\0'; uri ++)
if (ptr < (host + HTTP_MAX_URI - 1))
*ptr++ = *uri;
*ptr = '\0';
if (*uri == ':')
uri ++;
if (strchr(host, '.') != NULL || strchr(host, '/') != NULL || *uri == '\0')
{
if ((ptr = strchr(host, '/')) != NULL)
{
strlcpy(resource, ptr, HTTP_MAX_URI);
*ptr = '\0';
}
else
resource[0] = '\0';
if (isdigit(*uri & 255))
{
*port = strtol(uri, (char **)&uri, 10);
if (*uri == '/')
strlcpy(resource, uri, HTTP_MAX_URI);
}
else
*port = 631;
strcpy(method, "http");
username[0] = '\0';
return;
}
else
strlcpy(method, host, 32);
}
if (strncmp(uri, "//", 2) != 0)
{
strlcpy(resource, uri, HTTP_MAX_URI);
username[0] = '\0';
host[0] = '\0';
*port = 0;
return;
}
uri += 2;
if ((slash = strchr(uri, '/')) == NULL)
slash = uri + strlen(uri);
if ((atsign = strchr(uri, '@')) != NULL && atsign < slash)
{
for (ptr = username; uri < atsign; uri ++)
if (ptr < (username + HTTP_MAX_URI - 1))
{
if (*uri == '%' && isxdigit(uri[1] & 255) && isxdigit(uri[2] & 255))
{
uri ++;
if (isalpha(*uri))
quoted = (tolower(*uri) - 'a' + 10) << 4;
else
quoted = (*uri - '0') << 4;
uri ++;
if (isalpha(*uri))
quoted |= tolower(*uri) - 'a' + 10;
else
quoted |= *uri - '0';
*ptr++ = quoted;
}
else
*ptr++ = *uri;
}
*ptr = '\0';
uri = atsign + 1;
}
else
username[0] = '\0';
for (ptr = host; *uri != ':' && *uri != '/' && *uri != '\0'; uri ++)
if (ptr < (host + HTTP_MAX_URI - 1))
*ptr++ = *uri;
*ptr = '\0';
if (*uri != ':')
{
if (strcasecmp(method, "http") == 0)
*port = 80;
else if (strcasecmp(method, "https") == 0)
*port = 443;
else if (strcasecmp(method, "ipp") == 0)
*port = ippPort();
else if (strcasecmp(method, "lpd") == 0)
*port = 515;
else if (strcasecmp(method, "socket") == 0)
*port = 9100;
else
*port = 0;
}
else
{
*port = 0;
uri ++;
while (isdigit(*uri & 255))
{
*port = (*port * 10) + *uri - '0';
uri ++;
}
}
if (*uri == '\0')
{
resource[0] = '/';
resource[1] = '\0';
return;
}
strlcpy(resource, uri, HTTP_MAX_URI);
}
const char *
httpStatus(http_status_t status)
{
switch (status)
{
case HTTP_CONTINUE :
return ("Continue");
case HTTP_SWITCHING_PROTOCOLS :
return ("Switching Protocols");
case HTTP_OK :
return ("OK");
case HTTP_CREATED :
return ("Created");
case HTTP_ACCEPTED :
return ("Accepted");
case HTTP_NO_CONTENT :
return ("No Content");
case HTTP_NOT_MODIFIED :
return ("Not Modified");
case HTTP_BAD_REQUEST :
return ("Bad Request");
case HTTP_UNAUTHORIZED :
return ("Unauthorized");
case HTTP_FORBIDDEN :
return ("Forbidden");
case HTTP_NOT_FOUND :
return ("Not Found");
case HTTP_REQUEST_TOO_LARGE :
return ("Request Entity Too Large");
case HTTP_URI_TOO_LONG :
return ("URI Too Long");
case HTTP_UPGRADE_REQUIRED :
return ("Upgrade Required");
case HTTP_NOT_IMPLEMENTED :
return ("Not Implemented");
case HTTP_NOT_SUPPORTED :
return ("Not Supported");
default :
return ("Unknown");
}
}
#ifndef HAVE_HSTRERROR
const char *
cups_hstrerror(int error)
{
static const char * const errors[] =
{
"OK",
"Host not found.",
"Try again.",
"Unrecoverable lookup error.",
"No data associated with name."
};
if (error < 0 || error > 4)
return ("Unknown hostname lookup error.");
else
return (errors[error]);
}
#endif