Hi all. Ok, i got such php script:

<?php
header('Location: http://localhost/current/');
?>

Now i am sending GET request to this script with my C app and receiving following header:

HTTP/1.1 302 Found
Date: Sat, 20 Feb 2010 18:50:17 GMT
Server: Apache/2.2.9 (Ubuntu) PHP/5.2.6-2ubuntu4.5 with Suhosin-Patch
X-Powered-By: PHP/5.2.6-2ubuntu4.5
Location: http://localhost/current/
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html

So my question is, how to implement at this moment redirection to this new uri? Parse header, extract this uri and call same function from itself? Or use

goto

? By the way i was trying to parse it this way:

char *red = strstr(recvbuffer, "Location:");
	if(red != NULL){
	char *found, *uri;
	t = strtok(red,"\n");
	for(i = 0; t; t = strtok(NULL,"\n"), i++) token[i] = t;
	found = cut_it(token[0], " ");
	found = (found+8);
	puts(found);
	uri = strstr(found, "/");
	if(uri != NULL){
		printf("h: %s, p: %s\n", found, uri);
		}
	}

It should print at the end:
"h: localhost, p: /current/"
but instead it prints: ", p: /current/urrent/".
What is wrong with this code?
Here is my cut_it function:

char *cut_it( char *xx,  char *yy){
   if ( !*yy ) return xx;
   for ( ; *xx; ++xx ){
      if ( *xx == *yy ){
          char *h, *n;
         for ( h = xx, n = yy; *h && *n; ++h, ++n ){
            if ( *h != *n )break;
         }
         if ( !*n ){
            return xx;
         }}}
   return 0;
}

And i cannot use any 3rd party libs for parsing header - it should be in pure C. (I am working on Linux).
Thanks in advance

i would like to make an observation.

obscure variable names, cryptic logic, and peculiar bracket indentations do not lend your code to being easily read by the casual passer-by who might be inclined to scan your code for obvious or hidden errors.

this might also explain why for what's otherwise a trivial parsing exercise, it's been three days with zero comments.

although, with a little work, you might just have an entry for the next Obfuscated Code Contest

if you can just remember one thing: any hacker can type out some code and make it look complicated. a good programmer writes *readable* code and makes it look easy.

heh, thanks for reply.
I have actually fixed my code and made it work in an hour after posting topic here, there was some silly mistakes. Here is good one:

while((redirection_search = sys_strstr(tem, "Location:")) != (void*)0){
	CLEAR(recvbuffer);
	char *found, *uri;
	char con[512];
	CLEAR(con);
	t = sys_strtok(redirection_search,"\n");
	for(i = 0; t; t = sys_strtok((void*)0,"\n"), i++) token[i] = t;
	found = cut_it(token[0], " ");
	found = (found+8);
	sys_strcpy(con, found);
	sys_strtok(found,"/");
	uri = sys_strstr(con, "/");
	VisitSite(found, uri);
	break; 
}

CLEAR(var) => sys_memset(&var, 0x00, sizeof(var))
sys_ => my own syscalls based functions, libc equivalents, as i have stopped using libc.
VisitSite(found, uri) => calling function from itself
^^ this code handles as many redirections as it receives.
Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.