void cgiPrintTestPage(http_t *http, /* I - Connection to server */ const char *dest) /* I - Destination printer/class */ { ipp_t *request, /* IPP request */ *response; /* IPP response */ char uri[HTTP_MAX_URI], /* Printer URI */ resource[1024], /* POST resource path */ refresh[1024], /* Refresh URL */ filename[1024]; /* Test page filename */ const char *datadir; /* CUPS_DATADIR env var */ const char *user; /* Username */ /* * See who is logged in... */ user = getenv("REMOTE_USER"); /* * Locate the test page file... */ if ((datadir = getenv("CUPS_DATADIR")) == NULL) datadir = CUPS_DATADIR; snprintf(filename, sizeof(filename), "%s/data/testprint.ps", datadir); /* * Point to the printer/class... */ snprintf(resource, sizeof(resource), "/%s/%s", cgiGetVariable("SECTION"), dest); httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", ippPort(), "/%s/%s", cgiGetVariable("SECTION"), dest); /* * Build an IPP_PRINT_JOB request, which requires the following * attributes: * * attributes-charset * attributes-natural-language * printer-uri * requesting-user-name * document-format */ request = ippNewRequest(IPP_PRINT_JOB); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); if (user) ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, user); 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"); /* * Do the request and get back a response... */ if ((response = cupsDoFileRequest(http, request, resource, filename)) != NULL) { cgiSetIPPVars(response, NULL, NULL, NULL, 0); ippDelete(response); } if (cupsLastError() <= IPP_OK_CONFLICT) { /* * Automatically reload the printer status page... */ cgiFormEncode(uri, resource, sizeof(uri)); snprintf(refresh, sizeof(refresh), "2;URL=%s", uri); cgiSetVariable("refresh_page", refresh); } else if (cupsLastError() == IPP_NOT_AUTHORIZED) { puts("Status: 401\n"); exit(0); } cgiStartHTML(cgiText(_("Print Test Page"))); if (cupsLastError() > IPP_OK_CONFLICT) cgiShowIPPError(_("Unable to print test page:")); else { cgiSetVariable("PRINTER_NAME", dest); cgiCopyTemplateLang("test-page.tmpl"); } cgiEndHTML(); }
static void show_printer(http_t *http, /* I - Connection to server */ const char *printer) /* I - Name of printer */ { ipp_t *request, /* IPP request */ *response; /* IPP response */ ipp_attribute_t *attr; /* IPP attribute */ char uri[HTTP_MAX_URI]; /* Printer URI */ char refresh[1024]; /* Refresh URL */ fprintf(stderr, "DEBUG: show_printer(http=%p, printer=\"%s\")\n", http, printer ? printer : "(null)"); /* * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following * attributes: * * attributes-charset * attributes-natural-language * printer-uri */ request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", printer); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); cgiGetAttributes(request, "printer.tmpl"); /* * Do the request and get back a response... */ if ((response = cupsDoRequest(http, request, "/")) != NULL) { /* * Got the result; set the CGI variables and check the status of a * single-queue request... */ cgiSetIPPVars(response, NULL, NULL, NULL, 0); if (printer && (attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL && attr->values[0].integer == IPP_PRINTER_PROCESSING) { /* * Printer is processing - automatically refresh the page until we * are done printing... */ cgiFormEncode(uri, printer, sizeof(uri)); snprintf(refresh, sizeof(refresh), "10;URL=/printers/%s", uri); cgiSetVariable("refresh_page", refresh); } /* * Delete the response... */ ippDelete(response); /* * Show the standard header... */ cgiStartHTML(printer); /* * Show the printer status... */ cgiCopyTemplateLang("printer.tmpl"); /* * Show jobs for the specified printer... */ cgiCopyTemplateLang("printer-jobs-header.tmpl"); cgiShowJobs(http, printer); } else { /* * Show the IPP error... */ cgiStartHTML(printer); cgiShowIPPError(_("Unable to get printer status")); } cgiEndHTML(); }
void cgiPrintCommand(http_t *http, /* I - Connection to server */ const char *dest, /* I - Destination printer */ const char *command, /* I - Command to send */ const char *title) /* I - Page/job title */ { int job_id; /* Command file job */ char uri[HTTP_MAX_URI], /* Job URI */ resource[1024], /* Printer resource path */ refresh[1024], /* Refresh URL */ command_file[1024]; /* Command "file" */ http_status_t status; /* Document status */ cups_option_t hold_option; /* job-hold-until option */ const char *user; /* User name */ ipp_t *request, /* Get-Job-Attributes request */ *response; /* Get-Job-Attributes response */ ipp_attribute_t *attr; /* Current job attribute */ static const char * const job_attrs[] =/* Job attributes we want */ { "job-state", "job-printer-state-message" }; /* * Create the CUPS command file... */ snprintf(command_file, sizeof(command_file), "#CUPS-COMMAND\n%s\n", command); /* * Show status... */ if (cgiSupportsMultipart()) { cgiStartMultipart(); cgiStartHTML(title); cgiCopyTemplateLang("command.tmpl"); cgiEndHTML(); fflush(stdout); } /* * Send the command file job... */ hold_option.name = "job-hold-until"; hold_option.value = "no-hold"; if ((user = getenv("REMOTE_USER")) != NULL) cupsSetUser(user); else cupsSetUser("anonymous"); if ((job_id = cupsCreateJob(http, dest, title, 1, &hold_option)) < 1) { cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver"))); cgiSetVariable("ERROR", cupsLastErrorString()); cgiStartHTML(title); cgiCopyTemplateLang("error.tmpl"); cgiEndHTML(); if (cgiSupportsMultipart()) cgiEndMultipart(); return; } status = cupsStartDocument(http, dest, job_id, NULL, CUPS_FORMAT_COMMAND, 1); if (status == HTTP_CONTINUE) status = cupsWriteRequestData(http, command_file, strlen(command_file)); if (status == HTTP_CONTINUE) cupsFinishDocument(http, dest); if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE) { cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver"))); cgiSetVariable("ERROR", cupsLastErrorString()); cgiStartHTML(title); cgiCopyTemplateLang("error.tmpl"); cgiEndHTML(); if (cgiSupportsMultipart()) cgiEndMultipart(); cupsCancelJob(dest, job_id); return; } /* * Wait for the job to complete... */ if (cgiSupportsMultipart()) { for (;;) { /* * Get the current job state... */ snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id); request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); if (user) ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, user); ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", 2, NULL, job_attrs); if ((response = cupsDoRequest(http, request, "/")) != NULL) cgiSetIPPVars(response, NULL, NULL, NULL, 0); attr = ippFindAttribute(response, "job-state", IPP_TAG_ENUM); if (!attr || attr->values[0].integer >= IPP_JOB_STOPPED || attr->values[0].integer == IPP_JOB_HELD) { ippDelete(response); break; } /* * Job not complete, so update the status... */ ippDelete(response); cgiStartHTML(title); cgiCopyTemplateLang("command.tmpl"); cgiEndHTML(); fflush(stdout); sleep(5); } } /* * Send the final page that reloads the printer's page... */ snprintf(resource, sizeof(resource), "/printers/%s", dest); cgiFormEncode(uri, resource, sizeof(uri)); snprintf(refresh, sizeof(refresh), "5;URL=%s", uri); cgiSetVariable("refresh_page", refresh); cgiStartHTML(title); cgiCopyTemplateLang("command.tmpl"); cgiEndHTML(); if (cgiSupportsMultipart()) cgiEndMultipart(); }