#include "ipp-var.h"
int
main(int argc,
char *argv[])
{
cups_lang_t *language;
char *pclass;
http_t *http;
ipp_t *request,
*response;
ipp_attribute_t *attr;
ipp_status_t status;
char uri[HTTP_MAX_URI];
const char *which_jobs;
const char *op;
static const char *def_attrs[] =
{
"printer-name",
"printer-uri-supported"
};
cgiInitialize();
op = cgiGetVariable("OP");
language = cupsLangDefault();
http = httpConnectEncrypt("localhost", ippPort(), cupsEncryption());
printf("Content-Type: text/html;charset=%s\n\n", cupsLangEncoding(language));
ippSetServerVersion();
pclass = argv[0];
if (strcmp(pclass, "/") == 0 || strcmp(pclass, "classes.cgi") == 0)
{
pclass = NULL;
cgiSetVariable("TITLE", cupsLangString(language, CUPS_MSG_CLASS));
}
else
cgiSetVariable("TITLE", pclass);
cgiCopyTemplateLang(stdout, TEMPLATES, "header.tmpl", getenv("LANG"));
if (op == NULL || strcasecmp(op, "print-test-page") != 0)
{
request = ippNew();
request->request.op.operation_id = CUPS_GET_DEFAULT;
request->request.op.request_id = 1;
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
{
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
resource[HTTP_MAX_URI],
uri[HTTP_MAX_URI];
int port;
const char *server;
server = getenv("SERVER_NAME");
httpSeparate(attr->values[0].string.text, method, username,
hostname, &port, resource);
if (strcasecmp(hostname, server) == 0 &&
(strcmp(getenv("REMOTE_HOST"), "127.0.0.1") == 0 ||
strcmp(getenv("REMOTE_HOST"), "localhost") == 0 ||
strcmp(getenv("REMOTE_HOST"), server) == 0))
strcpy(hostname, "localhost");
snprintf(uri, sizeof(uri), "http://%s:%d%s", hostname, port,
resource);
cgiSetVariable("DEFAULT_URI", uri);
}
ippDelete(response);
}
request = ippNew();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
if (pclass == NULL)
{
request->request.op.operation_id = CUPS_GET_CLASSES;
request->request.op.request_id = 1;
}
else
{
request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
request->request.op.request_id = 1;
snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
pclass);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
uri);
}
ippGetAttributes(request, TEMPLATES, "classes.tmpl", getenv("LANG"));
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
ippSetCGIVars(response, NULL, NULL, NULL, 0);
ippDelete(response);
}
cgiCopyTemplateLang(stdout, TEMPLATES, "classes.tmpl", getenv("LANG"));
if (pclass != NULL)
{
request = ippNew();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
request->request.op.operation_id = IPP_GET_JOBS;
request->request.op.request_id = 1;
snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
pclass);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
uri);
if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
NULL, which_jobs);
ippGetAttributes(request, TEMPLATES, "jobs.tmpl", getenv("LANG"));
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
ippSetCGIVars(response, NULL, NULL, NULL, 0);
ippDelete(response);
cgiCopyTemplateLang(stdout, TEMPLATES, "jobs.tmpl", getenv("LANG"));
}
}
}
else
{
snprintf(uri, sizeof(uri), "ipp://localhost/classes/%s", pclass);
request = ippNew();
request->request.op.operation_id = IPP_PRINT_JOB;
request->request.op.request_id = 1;
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
if (getenv("REMOTE_USER") != NULL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, getenv("REMOTE_USER"));
else
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, "root");
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
NULL, "Test Page");
ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
NULL, "application/postscript");
if ((response = cupsDoFileRequest(http, request, uri + 15,
CUPS_DATADIR "/data/testprint.ps")) != NULL)
{
status = response->request.status.status_code;
ippSetCGIVars(response, NULL, NULL, NULL, 0);
ippDelete(response);
}
else
status = cupsLastError();
cgiSetVariable("PRINTER_NAME", pclass);
if (status > IPP_OK_CONFLICT)
{
cgiSetVariable("ERROR", ippErrorString(status));
cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
}
else
cgiCopyTemplateLang(stdout, TEMPLATES, "test-page.tmpl", getenv("LANG"));
}
cgiCopyTemplateLang(stdout, TEMPLATES, "trailer.tmpl", getenv("LANG"));
httpClose(http);
cupsLangFree(language);
return (0);
}