HTTPをサポートするCベースのサーバーを作成しています。 &バッファ[5]にファイル名があります。私は次のステップでヘッダーを設定し、8Kbのブロックでファイルを送信します。私はtxtファイルを要求しているときにうまくいきますが、jpgファイルを要求すると、o/pは与えられません。 イメージファイルに必要な特別な注意がありますか、その他の問題があります。 txtファイルのためのcでソケットを介して画像ファイルを送信する際の問題
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
:JPGファイルの
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] == '\r' || buffer[i] == '\n')
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: \n Parent directory (..) path names not supported",buffer,fd);
if(!strncmp(&buffer[0],"GET /\0",6) || !strncmp(&buffer[0],"get /\0",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\t%s\t%d\n", &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:\n file extension type not supported",buffer,fd);
printf("Buffer Value: %s\n",&buffer[5]);
if((file_fd = open(&buffer[5],O_RDONLY)) == -1) /* open the file for reading */
{
log1(SORRY, "HTTP/1.0 404 Not Found\n failed to open file",&buffer[5],fd);}
printf("file Descrptr:%d\n",file_fd);
log1(LOG,"SEND",&buffer[5],1);
(void)sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: %s\r\n\r\n", fstr);
(void)write(fd,buffer,strlen(buffer));
printf("Buffer Header:%s\n",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\n",strlen(buffer));
}
#ifdef LINUX
sleep(1); /* to allow socket to drain */
#endif
exit(1);
}
O/P
Buffer Value: config.txt
file Descrptr:4
Buffer Header:HTTP/1.0 200 OK
Content-Type: text/plain
BufferData:416
@ジョナサン...私はo/pで見ることができるように、私はHTTPヘッダーがきれいに印刷されました。 .jpgファイルのみで問題が発生しています......私は、Buffsizeを8Kbから高い値に上げるとスコープはありますか? – Glex
好きな方はURL http://sstatic.net/stackoverflow/img/apple-touch-icon.pngで遊ぶことができます。 –
あなたのレビューと回答におかげさまで、本当にありがとうございました。問題となった2つの重要なことは次のとおりです。1.コンテンツの長さが指定されていない...私はstat.hを使用し、ヘッダーにコンテンツの長さを入れました。 2. Content-Typeに書式設定の問題がありました。私が設定ファイルからタイプを読み込んでいたとき、私は '\ n'を取り除いていなかったので、そのフォーマットはHTTP stdと一致しませんでした..........このようなことを私に指摘してくれてありがとう...... :) – Glex