ERR_INVALID_CHUNKED_ENCODING for chunked transfer encoding for HTTP server

1.7k Views Asked by At

My task is to implement simple HTTP server with chunked transfer encoding. Here is function that read request and send response to the client.

 static int serve_request(int sock, struct conf_arg *arg, char version[])
    {
        int html;
        char buf[MAX_MSG];
        const unsigned chunk = (unsigned)CHUNK_SIZE;
        char tempbuf[CHUNK_SIZE + 3];
        ssize_t size;
        ssize_t bytes_read;

        strcpy(buf, arg->root);
        if(buf[strlen(buf) - 1] == '/')
            strcat(buf, arg->defdoc);
        html = open(buf, O_RDONLY);
        if (html == -1) {
            perror("Error in opening file");
            not_found(sock, version);
            return -1;
        }
        good_responce(sock, version); 
        size = lseek(html, 0, SEEK_END);
        lseek(html, 0, SEEK_SET);
        printf("SIZE:%d\n", (int)size);
        bytes_read = read(html, buf, size);
        while (bytes_read > chunk) {
            sprintf(tempbuf, "%x\r\n", (int)chunk);
            write(sock, tempbuf, strlen(tempbuf));
            write(sock, buf, chunk);
            write(sock, "\r\n", 2);
            bytes_read -= chunk;
        }
        sprintf(tempbuf, "%x\r\n", (int)bytes_read);
        printf("LAST PART:%d\n", (int)bytes_read);
        write(sock, tempbuf, strlen(tempbuf));
        write(sock, buf, bytes_read);
        write(sock, "\r\n", 2);
        strcpy(tempbuf, "0\r\n\r\n");
        write(sock, tempbuf, strlen(tempbuf));
        close(html);

        return 0;
    }

When I try to open simple site with my server I get error: ERR_INVALID_CHUNKED_ENCODING. So I hope you can help me to find the problem.

0

There are 0 best solutions below