Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 21.5 Source Code 835


699 if (jp->req.flags & PR_TEXT) {
700 p ="text/plain";
701 extra=1;
702 } else {
703 p ="application/postscript";
704 extra=0;
705 }
706 icp=add_option(icp, TAG_MIMETYPE, "document-format", p);
707 *icp++=TAG_END_OF_ATTR;
708 ilen=icp - ibuf;
709 /*
710 * Set up the HTTP header.
711 */
712 hcp=hbuf;
713 sprintf(hcp, "POST /ipp HTTP/1.1\r\n");
714 hcp += strlen(hcp);
715 sprintf(hcp, "Content-Length: %ld\r\n",
716 (long)sbuf.st_size +ilen + extra);
717 hcp += strlen(hcp);
718 strcpy(hcp, "Content-Type: application/ipp\r\n");
719 hcp += strlen(hcp);
720 sprintf(hcp, "Host: %s:%d\r\n", printer_name, IPP_PORT);
721 hcp += strlen(hcp);
722 *hcp++=’\r’;
723 *hcp++=’\n’;
724 hlen=hcp - hbuf;

[699 – 708] The last attribute we supply is thedocument-format.If we omit it, the
printer will interpret the file using some default format. For a PostScript
printer,this is probably PostScript, but some printers can autosense the
format and choose between PostScript, plaintext, or PCL (Hewlett-Packard’s
Printer Command Language). If thePR_TEXTflag is set, we set the format
totext/plain.Otherwise, we set it to application/postscript.
Then we delimit the end of the attributes portion of the header with an
end-of-attributes tag and calculate the size of the IPP header.
Theextrainteger counts any extra characters we might need to transmit to
the printer.As we shall see shortly, we need to send an extra character to be
able to print plain text reliably.Weneed to account for this extra character
when we calculate the content length.
[709 – 724] Now that we know the IPP header size, we can set up the HTTP header.We
set theContent-Lengthto the size in bytes of the IPP header plus the size
of the file to be printed plus any extra characters we might need to send.
TheContent-Typeisapplication/ipp.Wemark the end of the HTTP
header with a carriage return and a line feed. Finally, we calculate the size of
the HTTP header.
Free download pdf