1. goahead下载文件源码
static char *websIndex;
static char *websDocuments;
static void closeFile()
{
wfree(websIndex);
websIndex = NULL;
wfree(websDocuments);
websDocuments = NULL;
}
static void fileWriteEvent(Webs *wp)
{
char *buf;
ssize len, wrote;
assert(wp);
assert(websValid(wp));
if ((buf = walloc(ME_GOAHEAD_LIMIT_BUFFER)) == NULL) {
websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't get memory");
return;
}
while ((len = websPageReadData(wp, buf, ME_GOAHEAD_LIMIT_BUFFER)) > 0) {
if ((wrote = websWriteSocket(wp, buf, len)) < 0) {
break;
}
if (wrote != len) {
websPageSeek(wp, - (len - wrote), SEEK_CUR);
break;
}
}
wfree(buf);
if (len <= 0) {
websDone(wp);
}
}
static bool fileHandler(Webs *wp)
{
WebsFileInfo info;
char *tmp, *date;
ssize nchars;
int code;
char *pathfilename;
char *filenameExt;
char *filename;
char *disposition;
assert(websValid(wp));
assert(wp->method);
assert(wp->filename && wp->filename[0]);
pathfilename = websGetVar(wp, "filePath", NULL);
if (pathfilename==NULL)
return 1;
filename =sclone(getUrlLastSplit(sclone(pathfilename),"/"));
filenameExt =sclone(getUrlLastSplit(sclone(filename),"."));
if (wp->ext) wfree(wp->ext);
wp->ext=walloc(1+strlen(filenameExt)+1);
sprintf(wp->ext,".%s",sclone(filenameExt));
free(filenameExt);
filenameExt=NULL;
if (wp->filename) wfree(wp->filename);
wp->filename=sclone(pathfilename);
if (wp->path) wfree(wp->path);
wp->path=sclone(pathfilename);
#if !BIT_ROM
if (smatch(wp->method, "DELETE")) {
if (unlink(wp->filename) < 0) {
websError(wp, HTTP_CODE_NOT_FOUND, "Can't delete the URI");
} else {
websResponse(wp, 204, 0);
}
} else if (smatch(wp->method, "PUT")) {
websResponse(wp, wp->code, 0);
} else
#endif
{
if (websPageIsDirectory(wp)) {
nchars = strlen(wp->path);
if (wp->path[nchars - 1] == '/' || wp->path[nchars - 1] == '\\') {
wp->path[--nchars] = '\0';
}
tmp = sfmt("%s/%s", wp->path, websIndex);
websRedirect(wp, tmp);
wfree(tmp);
return 1;
}
if (websPageOpen(wp, O_RDONLY | O_BINARY, 0666) < 0) {
#if BIT_DEBUG
if (wp->referrer) {
trace(1, "From %s", wp->referrer);
}
#endif
websError(wp, HTTP_CODE_NOT_FOUND, "Cannot open document for: %s", wp->path);
return 1;
}
if (websPageStat(wp, &info) < 0) {
websError(wp, HTTP_CODE_NOT_FOUND, "Cannot stat page for URL");
return 1;
}
code = 200;
if (wp->since && info.mtime <= wp->since) {
code = 304;
}
websSetStatus(wp, code);
websWriteHeaders(wp, info.size, 0);
disposition = walloc(20+strlen(filename)+1);
sprintf(disposition,"attachment;filename=%s",sclone(filename));
websWriteHeader(wp, "Content-Disposition", sclone(disposition));
free(filename);
free(disposition);
filename=NULL;
disposition=NULL;
if ((date = websGetDateString(&info)) != NULL) {
websWriteHeader(wp, "Last-modified", "%s", date);
wfree(date);
}
websWriteEndHeaders(wp);
if (smatch(wp->method, "HEAD")) {
websDone(wp);
return 1;
}
websSetBackgroundWriter(wp, fileWriteEvent);
}
return 1;
}
/***
*goahead中的Action函数,前台可通过/action/downLoadFileAction访问
*/
static void downLoadFileAction(Webs *wp, char *path,char*query)
{
WebsHandlerProc service = (*wp).route->handler>service;
(*wp).route->handler->service =(*fileHandler);
(*wp).route->handler->service(wp);
(*wp).route->handler->service= service;
}