I am writing a C based server supporting HTTP. &buffer[5] has the file name. I set the header in next step and then send the file in block of 8Kb. It is working fine when I am requesting a txt file, but when I request a jpg file, it is not giving any o/p.
Is there any specific care I need to take for image file or there is some other problem.
void web(int fd)
{
int j, file_fd;
int buflen; int len;
long i, ret;
char * fstr;
static char buffer[BUFSIZE+1]; /* static so zero filled */
ret =read(fd,buffer,BUFSIZE); /* read Web request in one go */
if(ret == 0 || ret == -1) { /* read failure stop now */
//log1(SORRY,"failed to read browser request","",fd);
}
if(ret > 0 && ret < BUFSIZE) /* return code is valid chars */
buffer[ret]=0; /* terminate the buffer */
else buffer[0]=0;
for(i=0;i<ret;i++) /* remove CF and LF characters */
if(buffer[i] == '
' || buffer[i] == '
')
buffer[i]='*';
log1(LOG,"request",buffer,1);
if( strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4) )
log1(SORRY,"Only simple GET operation supported",buffer,fd);
for(i=4;i<BUFSIZE;i++) { /* null terminate after the second space to ignore extra stuff */
if(buffer[i] == ' ') { /* string is "GET URL " +lots of other stuff */
buffer[i] = 0;
break;
}
}
for(j=0;j<i-1;j++) /* check for illegal parent directory use .. */
if(buffer[j] == '.' && buffer[j+1] == '.')
log1(SORRY,"HTTP/1.0 400 Bad Request: Invalid URI:
Parent directory (..) path names not supported",buffer,fd);
if( !strncmp(&buffer[0],"GET /",6) || !strncmp(&buffer[0],"get /",6) ) /* convert no filename to index file */
(void)strcpy(buffer,"GET /index.html");
/* work out the file type and check we support it */
buflen=strlen(buffer);
fstr = (char *)0;
for(i=0;extensions[i].ext != 0;i++) {
len = strlen(extensions[i].ext);
printf("%s%s%d
", &buffer[buflen-len],extensions[i].ext, strncmp(&buffer[buflen-len], extensions[i].ext, len));
if( !strncmp(&buffer[buflen-len], extensions[i].ext, len)) {
fstr =extensions[i].filetype;
break;
}
}
if(fstr == 0)
log1(SORRY,"HTTP/1.0 400 Bad Request: Invalid URI:
file extension type not supported",buffer,fd);
printf("Buffer Value: %s
",&buffer[5]);
if(( file_fd = open(&buffer[5],O_RDONLY)) == -1) /* open the file for reading */
{
log1(SORRY, "HTTP/1.0 404 Not Found
failed to open file",&buffer[5],fd);}
printf("file Descrptr:%d
",file_fd);
log1(LOG,"SEND",&buffer[5],1);
(void)sprintf(buffer,"HTTP/1.0 200 OK
Content-Type: %s
", fstr);
(void)write(fd,buffer,strlen(buffer));
printf("Buffer Header:%s
",buffer);
//int rec = read(file_fd, buffer, 10*BUFSIZE);
/* send file in 8KB block - last block may be smaller */
while ( (ret = read(file_fd, buffer, BUFSIZE)) > 0 ) {
(void)write(fd,buffer,ret);
printf("BufferData:%d
",strlen(buffer));
}
#ifdef LINUX
sleep(1); /* to allow socket to drain */
#endif
exit(1);
}
o/p for jpg file:
server: got connection from 127.0.0.1
Buffer Header:HTTP/1.0 200 OK
Content-Type: image/jpeg
BufferData:4
BufferData:95
BufferData:122
BufferData:24
BufferData:217
for txt file:
Buffer Value: config.txt
file Descrptr:4
Buffer Header:HTTP/1.0 200 OK
Content-Type: text/plain
BufferData:416
See Question&Answers more detail:
os