get image header

Hello all inhabitant of discussion.
I try to take image header by http- protocol.
My get query listed below.
1
2
3
4
5
"GET /img/headlogo1.gif HTTP/1.1\r\n
Connection: keep-Alive\r\n
User-Agent: Opera/7.0 (Win32)\r\n
Host: cplusplus.com\r\n
Accept: image/* \r\n\r\n";

But i do't take it.
And how to possible any way to take only header a file image or not?
____________
Thanks in advance!
Hi quant.

it sounds like your problem lies in your GET request. Have you looked at the HTTP keyword "HEADERS" e.t. GET / HTTP/1.1 HEADERS. In any case you have made no references or statements that imply this is a C++ question.

Z.
Is my source below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main(){
struct sockaddr_in addr;
struct hostent * hostent;
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
hostent = gethostbyname("cplusplus.com");
addr.sin_addr.s_addr = *(u_int32_t*)hostent -> h_addr;
connect(socket_fd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in));
char buffer [4096] = "GET /img/headlogo1.gif HTTP/1.1\r\n
Connection: keep-Alive\r\n
User-Agent: Opera/7.0 (Win32)\r\n
Host: cplusplus.com\r\n
Accept: image/* \r\n\r\n";
write(socket_fd, buffer, 4096);
read(socket_fd, buffer2, 4096);
if((fopen(pFile, "w")) == NULL){
    // fputs ("test",pFile);
     fgets (buffer2,4096,pFile);
     puts (buffer2);
     fclose (pFile);
}

return 0;
}
Last edited on
I would do a strlen() of buffer to get the buffer size for the write call

 
write(socket_fd, buffer, strlen(buffer));


You are writing a full 4096 bytes including whatever is in your buffer after the GET string.

If you are reading a complete file is the read of 4096 enough or too much? I don't know how big the image data is.

Read will, by default block until, in your case it gets a full 4096 bytes. If what you are trying to read is not that big it will hang on this call. I would set the socket to non-blocking and pick up the read return value to see how much data it read in. Look at the man page for details of this and fcntl() for setting non-blocking.

If you don't know the size of the data to read or if you use a non-blocking socket then really the read call should be in a loop. Control the loop by the read return value, and make sure the buffer(s) can cope.
Last edited on
Well, I used strlen but I take such as warning. This is not dangerouse?
get_http.c:27: warnind: incompatible implicit declaration of built-in function 'strlen'
I don't tink, why code listen below geting only http-header without image file.
Like a BUFSIZ is long, or not?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char buffer [BUFSIZ] = "GET /img/headlogo1.gif HTTP/1.1\r\n
Connection: keep-Alive\r\n
User-Agent: Opera/7.0 (Win32)\r\n
Host: cplusplus.com\r\n
Accept: image/* \r\n\r\n";
char buffer2[BUFSIZ];
write(socket_fd, buffer, strlen(buffer));
read(socket_fd, buffer2);
printf("%s", buffer2);
FILE * pFile;
  pFile = fopen ("/usr/src/file.gif","w");
  if (pFile!=NULL)
  {
    fputs (buffer2,pFile);
    fclose (pFile);
}

First of all add #include <string.h> to get rid of the warning. That warning is to tell you the compiler doesn't know if you are passing the correct parameters to strlen().

I've compiled and run your code, and there is nothing wrong with it. I got the following response

HTTP/1.1 400 Bad Request
Date: Sat, 31 May 2008 08:02:43 GMT
Server: Apache/2.2.3 (CentOS)
Content-Length: 304
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at cplusplus.com Port 80</address>
</body></html>


You're sending an invalid request so it's not the code that is the problem
Yeah, I'm is understand in this. And I've is not problems to get a plain-text over by http request. I don't take just the image. I'm tray to get in again the image and detect GIF89a╪
How to any way parse this request and get only line into GIF89a╪? On unix-shell to use grep while on c++?
1
2
3
4
5
6
7
8
9
10
11
12
HTTP/1.1 200 OK
Content-Length: 1470
Content-Type: image/gif
Last-Modified: Thu, 10 Apr 2008 20:24:48 GMT
Accept-Ranges: bytes
ETag: "384425ec489bc81:589a"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Sat, 31 May 2008 16:15:00 GMT
Connection: close

GIF89a╪
Last edited on
There are a few things you could do but I think the most obvious one would be to put the above text in a std::string and use the member methods to extract what you want, maybe something like the example in http://www.cplusplus.com/reference/string/string/substr.html




Thanks!
I has a lot of one yet question. How to transport be cut buffer next method.
fwrite(buffer2 + i, 1, sz - i, pFile);
I'll hope to take inside pFile without reading a file. I want buffer3 with include 'buffer2 + i, 1, sz - i' value. The buffer3 should be string.
Please c, non c++ sample, without iostream to use.
_____
Thank in adv!
To do it in C rather than C++ you probably want to do something like this to find the GIF89 string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char *ptr = buffer2;
char temp[8];
char   found = 0; /* FALSE */

while (!found && *ptr != '\0')
{
   if (*ptr == 'G')
   {
       strncpy(temp, ptr, 7);
       temp[7] = '\0';
       if (strcmp(temp, "GIF89a╪") == 0)
       {
           found = 1; /* TRUE */
           ptr += 7;
           *ptr = '\0';
       }
   }
   else
   {
      ptr++;
   }
}


At this point buffer2 should termnate after the GIF89a╪ text. I have not compled or tested this code so make sure it works.
Last edited on
bnbertha: I can shorten your code ;) strstr();
http://www.cplusplus.com/reference/clibrary/cstring/strstr.html
Yep, forgot about that one. I'm a bit rusty on all those str* calls :P
Cool!
And how to any use regex? Example strcmp(temp, "GIF*)
GIF'and any extensions'
That is why I take any type images.
strstr()[code] will match a string within a given string. This is what you should use instead of [code]strcmp(temp, "GIF*").

strstr() will also return the location of the string you are looking for.
Strange, my strstr() to returning is a GIF* or not anything except by asterisk. For example after GIF be found any symbol.
In that case use strstr() to find 'GIF' then search a character at a time until you get to the end of line, a space or whatever marks the end of what you want. Then put it all together to make the string you want.
Topic archived. No new replies allowed.