ptg10805159
840 Communicating with a Network Printer Chapter 21
853 /*
854 * HTTP request was okay, but still need to check
855 * IPP status. Search for the Content-Length.
856 */
857 i = cp - bp;
858 for (;;) {
859 while (*cp != ’C’ && *cp != ’c’ && i < datsz) {
860 cp++;
861 i++;
862 }
863 if (i >= datsz) { /* get more header */
864 if ((nr = readmore(sfd, &bp, i, &bufsz)) < 0) {
865 goto out;
866 } else {
867 cp =&bp[i];
868 datsz += nr;
869 }
870 }
871 if (strncasecmp(cp, "Content-Length:", 15) == 0) {
872 cp += 15;
873 while (isspace((int)*cp))
874 cp++;
875 contentlen =cp;
876 while (isdigit((int)*cp))
877 cp++;
878 *cp++ =’\0’;
879 i = cp - bp;
880 len =atoi(contentlen);
881 break;
882 } else {
883 cp++;
884 i++;
885 }
886 }
[853 – 870] If the HTTP request succeeds, we need to check the IPP status. We search
through the message until we find theContent-Lengthattribute. HTTP
header keywords arecase insensitive, so we need to check both lowercase
and uppercase characters. If we run out of buffer space, we callreadmore,
which usesreallocto increase the buffer size. Because the buffer address
might change, we need to adjustcpto point to the correct place in the buffer.
[871 – 886] We use thestrncasecmpfunction to do a case-insensitive comparison. If
we find theContent-Lengthattribute string, we search for its value. We
convert this numeric string into an integer and break out of theforloop. If
the comparison fails, we continue searching the buffer byte by byte. If we
reach the end of the buffer without finding theContent-Lengthattribute,
we read morefromthe printer and continue the search.