ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Im studying C and unix network programming. Trying to make a cheap http server as i go. Anyway, i have a buffer setup to recieve the http client connect info, and I wanna be able to run a routine that responds to the (GET)'s that the server recieves. Basically I need the routine to search through variable buffer (which contains info below) and be able to extract the string like "nak_skull.JPG" from the GET below, to then send that file.
Contents of buffer variable -------------------------------------------------------------------
GET / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030630
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.
GET /nak_skull.JPG HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030630
Accept: video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1
etc.......
void parse_requests(char *buffer)
{
char str[4096], *p = buffer, *q;
while((p = strstr(p, "GET ")))
{
p += 4; // 4 comes from strlen("GET ");
if(!(q = strchr(p, ' ')))
q = p + strlen(p);
strncpy(str, p, q-p); // grab everything after space following GET to next space or end of buffer
str[q-p] = '\0';
handle_get(str);
}
}
The parse_requests() function should be passed that buffer that you mentioned. The handle_get() function should handle the security and sending of the individual request.
That works just like I hoped it would, but it displays /filename. I tried changing the ' ' to '/', but then it displays nothing. Anyway we can alter this or add something that removed the / from the beginning of the filename.
Sure, just change the "GET " strings to "GET /" in the code above and change the p += 4; to p += 5;
That should do the trick.
Just a note: the code I presented can be exploited by buffer overruns. If you mean for the server to be available to the public you might want to add something like 'if(q-p >= 4095) continue;' so there's no chance the strncpy() can overflow the str array. With that if() statement, any requests that are too long will just be ignored. Another approach would be to dynamically allocate memory for str instead of declaring it with a fixed length of 4096 (which was just some arbitrary number I pulled out of a hat anyway).
Hey thanks again. Im not actaually planning on running the server, I just wanna get the basics working now, then improve on it later. But ill note what u said.
I tried modifying the code as u said, changing "GET" to "GET /" and +=5. However, program still returns /filename. I tried modifying a few other things, like changing the ' ' to '/'. Tried a few other things also, but always still returns /filename.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.