int output_file(string fn, INT64 offset, INT64 length, int partial) { |
if (offset < 0) |
offset = 0; |
if (length < 0) |
length = 0; |
string_trim(fn); |
if (fn.size() == 0) |
fn = "/" ; |
//CFile in_file; |
//if(in_file.Open(fn.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) { |
//FILE * fp; |
CFile in_file; |
if (fn[fn.size()-1] == '/' ) { |
string idx; |
idx = fn + "index.html" ; |
//fp = fopen(idx.c_str(), "rb"); |
//if(fp == NULL) { |
if (in_file.Open(idx.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) { |
idx = fn + "index.htm" ; |
//fp = fopen(idx.c_str(), "rb"); |
//if(fp == NULL) { |
if (in_file.Open(idx.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) { |
printf ( "Param-Status: %d\r\n" , 404); |
printf ( "Param-Desc: %s\r\n" , NOT_FOUND); |
printf ( "Content-type: text/html\r\n" ); |
printf ( "Content-Length: %d\r\n" , strlen (NOT_FOUND)); |
printf ( "\r\n" ); |
printf ( "%s" , NOT_FOUND); |
|
return -1; |
} |
} |
} |
else { |
//fp = fopen(fn.c_str(), "rb"); |
//if(fp == NULL) { |
if (in_file.Open(fn.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) { |
printf ( "Param-Status: %d\r\n" , 404); |
printf ( "Param-Desc: %s\r\n" , NOT_FOUND); |
printf ( "Content-type: text/html\r\n" ); |
printf ( "Content-Length: %d\r\n" , strlen (NOT_FOUND)); |
printf ( "\r\n" ); |
printf ( "%s" , NOT_FOUND); |
|
return -1; |
} |
} |
string ext; |
if (fn.rfind( "." ) != string::npos) { |
ext = fn.substr(fn.rfind( "." )+1); |
} |
string_lower(ext); |
|
//printf("output_file()\n"); //debug |
//fseek(fp, 0, SEEK_END); |
//int flen = ftell(fp); |
INT64 flen = in_file.GetLength(); |
if (offset >= flen) { |
printf ( "Param-Status: %d\r\n" , 416); |
printf ( "Param-Desc: %s\r\n" , RANGE_INVALID); |
printf ( "Content-type: text/html\r\n" ); |
printf ( "Content-Length: %d\r\n" , strlen (RANGE_INVALID)); |
printf ( "\r\n" ); |
printf ( "%s" , RANGE_INVALID); |
|
return -1; |
} |
if (length > flen-offset || length == 0) |
length = flen-offset; |
int status = 200; |
if (length < flen || partial) { |
status = 206; |
} |
printf ( "Accept-Ranges: bytes\r\n" ); |
printf ( "Content-Length: %I64d\r\n" , length); |
printf ( "Content-type: %s\r\n" , get_mime(ext).c_str()); |
printf ( "Param-Status: %d\r\n" , status); |
if (status == 206) { |
printf ( "Param-Desc: %s\r\n" , PARTIAL_CONTENT); |
printf ( "Param-Range: bytes %I64d-%I64d/%I64d\r\n" , offset, offset+length-1, flen); |
} |
printf ( "\r\n" ); |
//fseek(fp, offset, SEEK_SET); |
in_file.Seek(offset, CFile::begin); |
|
char buf[100*1024]; |
INT64 read_len = 0; |
//while(!feof(fp) && read_len < length) { |
while (in_file.GetPosition() < in_file.GetLength() && read_len < length) { |
/* |
int buf_len = sizeof(buf); |
if(buf_len > length-read_len) { |
buf_len = length-read_len; |
} |
*/ |
//int ret = fread(buf, buf_len, 1, fp); |
int len = in_file.Read(buf, sizeof (buf)); |
if (len > 0) { |
fwrite (buf, len, 1, stdout); |
} |
else { |
break ; |
} |
read_len += len; |
} |
//fclose(fp); |
in_file.Close(); |
return 0; |
} |
#endif |
unsigned int xrand() |
{ |
unsigned int num = ( rand () )* ( rand ()); |
return num; |
} |
string get_sign( const string & val) { |
string key; |
key.assign(( const char *)KEY, 16); |
string sign = md5(val + key); |
return sign; |
} |
int output_http_header( int status, string desc, string mime = "text/html" ) { |
//_SETmode( _fileno( stdout ), _O_BINARY ); |
|
printf ( "Param-Status: %d\r\n" , status); |
printf ( "Param-Desc: %s\r\n" , desc.c_str()); |
printf ( "Content-type: %s\r\n" , mime.c_str()); |
printf ( "Content-Length: %d\r\n" , strlen (desc.c_str())); // note: desc.size() is different from strlen(desc.c_str()) |
printf ( "\r\n" ); |
printf ( "%s" , desc.c_str()); |
return 0; |
} |
初级程序员
by: 是一只小龙呀 发表于:2021-11-11 19:40:34 顶(0) | 踩(0) 回复
1
回复评论