Thread: Sending png to browser

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    19

    Sending png to browser

    I'm trying to do a small web server. I want this server to send a png.

    The client browser sends the request:

    Code:
    GET /someimage.png HTTP/1.1
    
    loads of information I don't care about
    The server receives and understands the request, however I can't send a png with success.

    I'm doing it like this:
    Code:
        char * buf= calloc(FILE_BUF_SIZE + 40, sizeof(char));
        int fd;
        char file[FILE_BUF_SIZE];
        char * filetype;
    
        bzero(&file, FILE_BUF_SIZE);
    
        filetype= file_type(file_addr);
    
    
    
        fd= open(file_addr, O_RDONLY);
    
        if(fd < 0)
            perror("error opening file");
    
        if(read(fd, file, FILE_BUF_SIZE) < 0)
            perror("error reading file");
    
        close(fd);
    
    
        sprintf(buf, "Content-Type: %s\n\n%s", filetype, file);
    Then some other function adds "HTTP/1.1 200 OK" and the final message should look like:

    Code:
    HTTP/1.1 200 OK
    Content-Type: image/png
    
    png file completely raw
    \r\n
    Errors I might think I might have:

    1- I don't know if the \r\n should go on another message

    2- The png might need to be processed before sending it to the client browser

    With this same code applied to html files I was able to send and present it correctly on the browser

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    19
    Also the FILE_BUF_SIZE is currently set as 10k. The png I am trying to send is 3.5k, so there should be no problem there

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    19
    This is the function that sends the message to the client:
    Code:
    void send_file(int remotefd, char file_addr[], int http_subver){
    
        char *file;
        char buf[FILE_BUF_SIZE + 60];
    
        bzero(buf, FILE_BUF_SIZE + 60);
    
        file= read_file(file_addr);
    
        sprintf(buf, "HTTP/1.%d 200 OK\n", http_subver);
    
        strncat(buf, file, FILE_BUF_SIZE+40);
    
        free(file);
    
        write(remotefd, buf, strlen(buf));
    
        printf("%s", buf);
    
        return;
    }
    This is the sub function that builds the message:
    Code:
    #define FILE_BUF_SIZE 1024*10 //10k
    #define SEND_FILE "Content-Type: %s\n\n%s\n\r\n"
    
    
    char * read_file(char * file_addr){
    
        char * buf= calloc(FILE_BUF_SIZE + 40, sizeof(char));
        int fd;
        char file[FILE_BUF_SIZE];
        char * filetype;
    
    
        bzero(&file, FILE_BUF_SIZE);
    
        filetype= file_type(file_addr);
    
    
    
        fd= open(file_addr, O_RDONLY);
    
        if(fd < 0)
            perror("error opening file");
    
        if(read(fd, file, FILE_BUF_SIZE) < 0)
            perror("error reading file");
    
        close(fd);
    
    
        sprintf(buf, SEND_FILE, filetype, file);
    
    
    
        return buf;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What Browser do you use?
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 03-26-2009, 02:18 PM
  2. browser
    By munna_dude in forum C Programming
    Replies: 1
    Last Post: 05-18-2007, 05:49 AM
  3. Browser Help
    By mach5 in forum Windows Programming
    Replies: 6
    Last Post: 02-26-2003, 01:32 PM
  4. Please Try My Web Browser
    By Daniel in forum Windows Programming
    Replies: 10
    Last Post: 06-20-2002, 11:02 AM
  5. Help from anybody using XP (Probably just IE 6 browser)
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-28-2002, 09:16 PM