LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2004, 05:20 PM   #1
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Rep: Reputation: 15
C String Help


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.......

-------------------------------------------------------------------------------------------------
THANKS!!!!!!!!!!!!!!!!

PS....If there is a better way to do this altogether...please let me know.

 
Old 04-13-2004, 06:33 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You could try something like this:

Code:
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.
 
Old 04-13-2004, 08:27 PM   #3
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
THANKS!!

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.

Thanks again,
 
Old 04-13-2004, 11:27 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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).

Last edited by itsme86; 04-13-2004 at 11:32 PM.
 
Old 04-14-2004, 11:23 AM   #5
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
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.

Thanks again.
 
Old 04-14-2004, 11:55 AM   #6
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 131

Original Poster
Rep: Reputation: 15
Me again,

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.

Thanks!
 
Old 04-14-2004, 12:47 PM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Hmm, it works fine for me.

Code:
#include <stdio.h>
#include <string.h>

void handle_get(char *str)
{
  printf("Handling request for file: %s\n", str);
}

void parse_requests(char *buffer)
{
  char str[4096], *p = buffer, *q;

  while((p = strstr(p, "GET /")))
  {
    p += 5;  // 5 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
    str[q-p] = '\0';

    handle_get(str);
  }
}

int main(void)
{
  char buffer[] = "GET /nak_skull.JPG HTTP/1.1";

  parse_requests(buffer);

  return 0;
}
and then:

itsme@dreams:~/C$ ./http
Handling request for file: nak_skull.JPG
itsme@dreams:~/C$
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
string huno Programming 2 05-22-2005 03:17 PM
[c] string help saiz66 Programming 4 10-06-2004 01:00 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration