From: Mr_John on
im trying with web server i wrote in unix (Freebsd) to send file to
mozilla client.it works fine with html,txt.. but when i try to send a
jpeg, mozilla browser write: " This image 127.0.0.1:1024/pic.jpg
cannot be displayed because it contains an error".where is my error in
the code i think there could be some byte in the buffer it cannot
read.but i can find where im wrong in the code..please someone can
help me?thanks a lot


char *risp; /*buffer when i put the file before sending*/

/* i get the n with getfilesize() correctly tested*/

risp=malloc(n);

strncpy(msgb,"HTTP/1.0 200 OK\r\nContent-Type:image/jpeg\r\nContent-
Length:21139\r\nServer: webserver/0.1 (FreeBSD)\r\n\r
\n",sizeof(msgb)-1); /*i write an header just to try if works*/

msgb[199]='\0';

fp = fopen(uri,"r");/*uri is ok (tested) */
if(fp==NULL)printf("Error opening file");
else{
ret=fread(risp,1,n,fp);
risp[n]='\0';

ret=send(cl_sk,msgb,strlen(msgb),0);
ret=send(cl_sk,risp,strlen(risp),0);

fclose(fp);
free(risp);
From: Jens Thoms Toerring on
Mr_John <lecter.hannibal(a)email.it> wrote:
> im trying with web server i wrote in unix (Freebsd) to send file to
> mozilla client.it works fine with html,txt.. but when i try to send a
> jpeg, mozilla browser write: " This image 127.0.0.1:1024/pic.jpg
> cannot be displayed because it contains an error".where is my error in
> the code i think there could be some byte in the buffer it cannot
> read.but i can find where im wrong in the code..please someone can
> help me?thanks a lot


> char *risp; /*buffer when i put the file before sending*/

> /* i get the n with getfilesize() correctly tested*/

> risp=malloc(n);

> strncpy(msgb,"HTTP/1.0 200 OK\r\nContent-Type:image/jpeg\r\nContent-
> Length:21139\r\nServer: webserver/0.1 (FreeBSD)\r\n\r
> \n",sizeof(msgb)-1); /*i write an header just to try if works*/

> msgb[199]='\0';

> fp = fopen(uri,"r");/*uri is ok (tested) */
> if(fp==NULL)printf("Error opening file");
> else{
> ret=fread(risp,1,n,fp);
> risp[n]='\0';

That's already problematic because you alllocated only 'n'
bytes but now you write one more byte. Remember: if you
write to risp[n] you assign to the (n+1)th element of the
buffer.

> ret=send(cl_sk,msgb,strlen(msgb),0);
> ret=send(cl_sk,risp,strlen(risp),0);

I guess what's going to be in 'risp' is data from the jpeg file.
And that's not a string but binary data, which very well can
contain '\0' characters all over the place. So adding a '\0'
at the end of what you read and using strlen() on it doesn't
make sense (strlen() stops at the first '\0' it encounters,
not necessarily at the end of the file). Just do

ret = send( cl_sk, risp, n, 0 );

instead if 'n' is the size of the file.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: David Schwartz on
On Jun 18, 4:01 pm, Mr_John <lecter.hanni...(a)email.it> wrote:

>                        ret=fread(risp,1,n,fp);
>                        risp[n]='\0';
>
>                     ret=send(cl_sk,msgb,strlen(msgb),0);
>                     ret=send(cl_sk,risp,strlen(risp),0);

Umm, 'strlen' is for C-style strings, not for arbitrary data.

DS
From: James Kanze on
On Jun 19, 1:01 am, Mr_John <lecter.hanni...(a)email.it> wrote:
> im trying with web server i wrote in unix (Freebsd) to send
> file to mozilla client.it works fine with html,txt.. but when
> i try to send a jpeg, mozilla browser write: " This image
> 127.0.0.1:1024/pic.jpg cannot be displayed because it contains
> an error".where is my error in the code i think there could be
> some byte in the buffer it cannot read.but i can find where im
> wrong in the code..please someone can help me?thanks a lot

> char *risp; /*buffer when i put the file before sending*/

> /* i get the n with getfilesize() correctly tested*/

> risp=malloc(n);

> strncpy(msgb,"HTTP/1.0 200 OK\r\nContent-Type:image/jpeg\r\nContent-
> Length:21139\r\nServer: webserver/0.1 (FreeBSD)\r\n\r
> \n",sizeof(msgb)-1); /*i write an header just to try if works*/

> msgb[199]='\0';

> fp = fopen(uri,"r");/*uri is ok (tested) */

Just a nit, but formally, you can't read binary data if you open
the file in text mode (but it doesn't matter in Unix).

> if(fp==NULL)printf("Error opening file");
> else{
> ret=fread(risp,1,n,fp);
> risp[n]='\0';

> ret=send(cl_sk,msgb,strlen(msgb),0);
> ret=send(cl_sk,risp,strlen(risp),0);

And it makes no sense using strlen on binary data.

Quite frankly, for binary data, I'd use open and read, then send
the number of bytes returned from the read request.

Text data is somewhat more complicated, because in theory, on
the Internet you're supposed to use CRLF as line end, instead of
just LF. In practice, however, most HTTP clients will be
tolerant enough to accept either.

> fclose(fp);
> free(risp);

--
James Kanze (GABI Software) email:james.kanze(a)gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34