c programming http get request

This is a discussion on c programming http get request within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to work out how to write a web server program that will serve a single file ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    c programming http get request

    Hello,
    I am trying to work out how to write a web server program that will serve a single file via a HTTP GET request if the file exists otherwise give the user a 404 error.

    I have started by figuring out what libraries I would need, the size of the buffer and a basic struct

    Code:
    #include <stdio.h>
    #include <stdlib.h>
        #include <unistd.h>
        #include <errno.h>
        #include <string.h>
        #include <fcntl.h>
        #include <signal.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>
    #define BUFZ 8096;
    struct
        {
           char *file_type;
           char *ext;
           
        }
        extensions [] =
        {
           {"gif", "image/gif" }, 
           {"jpg", "image/jpeg"},
           {"jpeg","image/jpeg"},
           {"png", "image/png" },   
           {"htm", "text/html" }, 
           {"html","text/html" }, 
           {0,0}
        };
    I am not sure exactly where to take it from here. I am trying to get the web server to listen on port 10076 and requests should be outputted in the browser, through the network card ie the request would be http://ipofethcard:10076/path/file.html

    Any ideas on how to achieve this or what functions I would need to look at? Perhaps an example code/skeleton I could refer to?

    Thanks for any suggestions,
    Last edited by daza166; 02-16-2011 at 02:18 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, you're going to have to learn sockets programming. You will need an understanding of the HTTP protocal. And you're going to need some depth in TCP/IP... and of course you'll need to know C pretty well in the bargain...

    A list of includes and a pre-loaded list of file extensions doesn't even scratch the surface on a project like this.

    Here's a few links to get you started....

    Linux Howtos: C/C++ -> Sockets Tutorial
    Winsock Programmer&rsquo;s FAQ: Winsock Programmer's FAQ

    HTTP Made Really Easy
    RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Or you could use libcurl.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending http get request
    By LeeVerr in forum C Programming
    Replies: 4
    Last Post: 09-07-2010, 09:02 PM
  2. HTTP GET request
    By aosmith in forum C Programming
    Replies: 4
    Last Post: 03-21-2010, 07:00 AM
  3. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  4. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21