Пример #1
0
int readallresponce(int fd, int *keepalive)
{
     char c, cprev, lbuf[SIZE_8K];
     int len, remains, toread, i, totalbytes;
     i = 0;
     totalbytes = 0;
     *keepalive = 1;
     while (1) {
          totalbytes += icap_readline(fd, lbuf, SIZE_8K);
          if (i == 0 && strstr(lbuf, "Connection: close"))
               *keepalive = 0;
          if (strlen(lbuf) == 0)
               i++;
          if (i == 2)
               break;
     }

     while (1) {
          c = 0;
          cprev = 0;
          for (i = 0; !(c == '\n' && cprev == '\r'); i++) {
               cprev = c;
               if (i > 10 || read(fd, &c, 1) <= 0) {
                    lbuf[i] = '\0';
                    printf("Error reading socket %d.Exiting i:%d, lbuf:%s\n",
                           fd, i, lbuf);
                    return -1;
               }
               totalbytes += 1;
               lbuf[i] = c;
          }
          len = strtol(lbuf, NULL, 16);
//        printf("Going to read %d bytes\n",len);
          if (len == 0) {
               read(fd, &c, 1);
               read(fd, &c, 1);
               return totalbytes + 2;
          }
          totalbytes += len;
          remains = len + 2;
          while (remains > 0) {
               toread = (remains > SIZE_8K ? SIZE_8K : remains);
               if ((len = read(fd, lbuf, toread)) <= 0)
                    return -1;
               remains = remains - len;
          }
     }
     return 1;
}
Пример #2
0
int readallresponce(int fd)
{
    char c, cprev, lbuf[512];
    int len, bytes, remains, toread, i, totalbytes;
    i = 0;
    while (1) {
        icap_readline(fd, lbuf, 512);
        if (strlen(lbuf) == 0)
            i++;
        if (i == 2)
            break;
    }
    printf("OK headers readed...\n");
    totalbytes = 0;
    while (1) {
        c = 0;
        cprev = 0;
        for (i = 0; !(c == '\n' && cprev == '\r'); i++) {
            cprev = c;
            if (i > 10 || read(fd, &c, 1) <= 0) {
                lbuf[i] = '\0';
                printf("Error.Exiting i:%d, lbuf:%s", i, lbuf);
                return 0;
            }
            lbuf[i] = c;
        }
        len = strtol(lbuf, NULL, 16);
//        printf("Going to read %d bytes\n",len);
        if (len == 0) {
            read(fd, &c, 1);
            read(fd, &c, 1);
            return totalbytes;
        }
        totalbytes += len;
        remains = len + 2;
        while (remains > 0) {
            toread = (remains > 512 ? 512 : remains);
            len = read(fd, lbuf, toread);
            remains = remains - len;
        }
    }

}