int Webdav_Props(HashHandle hash, CacheHandle cache, char * webpath, int depth) { char * filename = NULL; char * temppath = webpath; WEBLOG_INFO("<%s> path=\"%s\", depth=%d.\n", __FUNCTION__, webpath, depth); /* xml head. */ CacheWriteString(cache, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<D:multistatus xmlns:D=\"DAV:\">\n"); if(depth == 0) { _GetObjectProps(hash, cache, temppath); } else { /* Here is the magic! */ ______FOR_EACH_FILE_IN(temppath, filename); _GetObjectProps(hash, cache, filename); ______END_FOR_EACH(); } /* xml foot. */ CacheWriteString(cache, "</D:multistatus>\n"); return WEBDAVR_OK; __error: return WEBDAVR_NG; }
bool wxHtmlHelpData::SaveCachedBook(wxHtmlBookRecord *book, wxOutputStream *f) { int i; wxInt32 cnt; /* save header - version info : */ CacheWriteInt32(f, CURRENT_CACHED_BOOK_VERSION); CacheWriteInt32(f, CACHED_BOOK_FORMAT_FLAGS); /* save contents : */ int len = m_contents.size(); for (cnt = 0, i = 0; i < len; i++) if (m_contents[i].book == book && m_contents[i].level > 0) cnt++; CacheWriteInt32(f, cnt); for (i = 0; i < len; i++) { if (m_contents[i].book != book || m_contents[i].level == 0) continue; CacheWriteInt32(f, m_contents[i].level); CacheWriteInt32(f, m_contents[i].id); CacheWriteString(f, m_contents[i].name); CacheWriteString(f, m_contents[i].page); } /* save index : */ len = m_index.size(); for (cnt = 0, i = 0; i < len; i++) if (m_index[i].book == book && m_index[i].level > 0) cnt++; CacheWriteInt32(f, cnt); for (i = 0; i < len; i++) { if (m_index[i].book != book || m_index[i].level == 0) continue; CacheWriteString(f, m_index[i].name); CacheWriteString(f, m_index[i].page); CacheWriteInt32(f, m_index[i].level); // save distance to parent item, if any: if (m_index[i].parent == NULL) { CacheWriteInt32(f, 0); } else { int cnt2 = 0; wxHtmlHelpDataItem *parent = m_index[i].parent; for (int j = i-1; j >= 0; j--) { if (m_index[j].book == book && m_index[j].level > 0) cnt2++; if (&m_index[j] == parent) break; } wxASSERT(cnt2 > 0); CacheWriteInt32(f, cnt2); } } return true; }
int Webdav_List(CacheHandle cache, char * ospath) { char webpath[256] = {0}; char strbuf[256] = {0}; char enc_url[256] = {0}; char * subospath = NULL; HashElement * e = NULL; char * p = NULL; WEBLOG_INFO("<%s> ospath=\"%s\".\n", __FUNCTION__, ospath); strcpy(webpath, ospath+strlen(ROOT_PATH)); if(0 == webpath[0]) strcat(webpath, "/"); WEBLOG_INFO("<%s> webpath=\"%s\".\n", __FUNCTION__, webpath); CacheWriteString(cache, "<html><head><title>Folder View</title></head><body>\n" "<h1>Index of "); CacheWriteString(cache, webpath); CacheWriteString(cache, "</h1>\n<ul>\n"); CacheWriteString(cache, "<li><a href=\"/"); /* calculate parent folder */ strcpy(strbuf, webpath); if(TRUE == endwith(strbuf, "/")) strbuf[strlen(strbuf)-1] = 0; if(0 != strlen(strbuf)) { p = strrchr(strbuf, '/'); if(p && p != strbuf) { *p = 0; CacheWriteString(cache, strbuf); CacheWriteString(cache, "/"); } } CacheWriteString(cache, "\"> Parent Directory</a></li>\n"); /* Here is the magic! */ ______FOR_EACH_FILE_IN(ospath, subospath); /* find relative webpath */ WEBLOG_INFO("<%s> strbuf \"%s\", subospath \"%s\", webpath \"%s\", ospath \"%s\". \n", __FUNCTION__, strbuf, subospath, webpath, ospath); strcpy(strbuf, strstr(subospath, ospath)+strlen(ospath)); WEBLOG_INFO("<%s> strbuf \"%s\", subospath \"%s\", webpath \"%s\", ospath \"%s\". \n", __FUNCTION__, strbuf, subospath, webpath, ospath); if(TRUE == Webdav_isFolder(subospath)) { CacheWriteString(cache,"<li> +"); } else { CacheWriteString(cache,"<li> "); } CacheWriteString(cache, "<a href=\""); memset(enc_url, 0, sizeof(enc_url)); url_encode(subospath, enc_url); CacheWriteString(cache, enc_url); CacheWriteString(cache, "\">"); CacheWriteString(cache, strbuf); CacheWriteString(cache, "</a></li>\n"); ______END_FOR_EACH(); CacheWriteString(cache,"</ul>\n</body></html>"); return WEBDAVR_OK; __error: return WEBDAVR_NG; }
static int _GetObjectProps(HashHandle hash, CacheHandle cache, char * webpath) { int ret = 0; char localpath[128] = {0}; char strbuf[128] = {0}; HashElement * e = NULL; int directory = FALSE; struct stat filestt; char last_modify_time[100] = {0}; getlocalpath(webpath, localpath); WEBLOG_INFO("<%s> [w]%s, [l]%s.\n", __FUNCTION__, webpath, localpath); ret = stat(localpath, &filestt); if(ret != 0) { switch (errno) { case ENOENT: WEBLOG_INFO("<%s> File %s not found.\n", __FUNCTION__, localpath); return WEBDAVR_NOT_FOUND; case EINVAL: default: WEBLOG_INFO("<%s> Unexpected error in _stat.\n", __FUNCTION__); return WEBDAVR_NG; } } directory = (S_IFDIR & filestt.st_mode); WEBLOG_INFO("<%s> %s is a %s.\n", __FUNCTION__, localpath, directory?"folder":"file"); if(directory) { HashAdd(hash, "resourcetype", HashString("<D:collection/>", TRUE)); HashAdd(hash, "getcontenttype", HashString("httpd/unix-directory", TRUE)); HashAdd(hash, "getcontentlength", HashString("", TRUE)); } else { //HashAdd(hash, "resourcetype", HashString("", TRUE)); // empty this HashAdd(hash, "getcontenttype", HashString("application/octet-stream", TRUE)); sprintf(strbuf, "%ld", filestt.st_size); WEBLOG_INFO("<%s> File size : %ld\n", __FUNCTION__, filestt.st_size ); HashAdd(hash, "getcontentlength", HashString(strbuf, TRUE)); } WEBLOG_INFO("<%s> Time modified : %s", __FUNCTION__, ctime(&filestt.st_mtime)); //HashAdd(hash, "getlastmodified", HashString(ctime(&filestt.st_mtime), TRUE)); //HashAdd(hash, "getlastmodified", HashString("Thu, 28 Mar 2013 06:30:48 GMT", TRUE)); strftime( last_modify_time, sizeof(last_modify_time), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&filestt.st_mtime)); HashAdd(hash, "getlastmodified", HashString(last_modify_time, TRUE)); CacheWriteString(cache, "\t<D:response>\n" "\t\t<D:href>"); CacheWriteString(cache, "/webdav"); memset(strbuf, 0, sizeof(strbuf)); url_encode(webpath, strbuf); CacheWriteString(cache, strbuf); CacheWriteString(cache, "</D:href>\n"); /* first comes valid props. */ CacheWriteString(cache, "\t\t<D:propstat>\n" "\t\t\t<D:prop>\n"); e = HashFirst(hash); while(e) { if(e->value.data.string[0]) { CacheWriteString(cache, "\t\t\t\t<D:"); CacheWriteString(cache, e->name); CacheWriteString(cache, ">"); CacheWriteString(cache, e->value.data.string); // just for test CacheWriteString(cache, "</D:"); CacheWriteString(cache, e->name); CacheWriteString(cache, ">\n"); } e = HashNext(hash, e); } CacheWriteString(cache, "\t\t\t</D:prop>\n" "\t\t\t<D:status>"); CacheWriteString(cache, "HTTP/1.1 200 OK"); CacheWriteString(cache, "</D:status>\n" "\t\t</D:propstat>\n"); /* then invalid props. */ CacheWriteString(cache, "\t\t<D:propstat>\n" "\t\t\t<D:prop>\n"); e = HashFirst(hash); while(e) { if(!e->value.data.string[0]) { CacheWriteString(cache, "\t\t\t\t<D:"); CacheWriteString(cache, e->name); CacheWriteString(cache, "/>\n"); } e = HashNext(hash, e); }; CacheWriteString(cache, "\t\t\t</D:prop>\n" "\t\t\t<D:status>"); CacheWriteString(cache, "HTTP/1.1 404 Not Found"); CacheWriteString(cache, "</D:status>\n" "\t\t</D:propstat>\n"); CacheWriteString(cache, "\t</D:response>\n"); return WEBDAVR_OK; }
int http_send_response(http_rsp * rsp) { CacheHandle cache = NULL_HANDLE; char * datapointer = NULL; int datalength = 0; char strbuf[64] = {0}; char bodybuf[256] = {0}; HashElement * e = NULL; http_status * stt = get_http_status(rsp->code); WEBLOG_API(); ASSERT(stt); if(using_customized_body != rsp->body) { /* if body is empty, make a default body. */ if(!rsp->body) { WEBLOG_VERBOSE("<%s> body empty. add default body.\n", __FUNCTION__); sprintf(bodybuf, "<HTML><BODY><b>%s</b></BODY></HTML>", stt->s_name); rsp->body = bodybuf; http_add_rspoption(rsp, "Content-Type", "text/html"); } /* if Content-Length is not given, try caculating it here. */ if(!HashLookup(rsp->headOptions, "Content-Length")) { if(rsp->body) sprintf(strbuf, sizett, strlen(rsp->body)); else sprintf(strbuf, "%d", 0); http_add_rspoption(rsp, "Content-Length", strbuf); } } /* server info is always present. */ http_add_rspoption(rsp, "Server", SERVER_VER); #ifdef WEBCFG_SUPPORT_HTTPRANGE http_add_rspoption(rsp, "Accept-Ranges", "bytes"); #endif if(NULL_HANDLE == cache) cache = CacheCreate(); ASSERT(NULL_HANDLE != cache); CacheClean(cache); /* send first line! */ CacheWriteString(cache, "HTTP/1.1 "); CacheWriteString(cache, stt->s_code); CacheWriteString(cache, " "); CacheWriteString(cache, stt->s_name); CacheWriteString(cache, "\r\n"); //HashDump(rsp->headOptions); /* send head options! */ e = HashFirst(rsp->headOptions); while(e) { CacheWriteString(cache, e->name); CacheWriteString(cache, ": "); if(e->value.type == eHashString) { CacheWriteString(cache, e->value.data.string); } else if(e->value.type == eHashInt) { sprintf(strbuf, "%d", e->value.data.integer); CacheWriteString(cache, strbuf); } CacheWriteString(cache, "\r\n"); e = HashNext(rsp->headOptions, e); } CacheWriteString(cache, "\r\n"); /* send body */ if(rsp->body && rsp->body != using_customized_body) CacheWriteString(cache, rsp->body); datalength = CacheGetData(cache, &datapointer); ASSERT(datapointer && datalength > 0); WebWrite(rsp->sock, datapointer, datalength); CacheDestroy(cache); return OK; }
int web_lsdir(CacheHandle cache, char * webpath) { char ospath[256] = {0}; char strbuf[256] = {0}; char subwebpath[256] = {0}; char enc_url[256] = {0}; char * p = NULL; DIR * dir = NULL; struct dirent *ent = NULL; int isdir = 0; unifypath(webpath); WEBLOG_INFO("<%s> webpath=\"%s\".\n", __FUNCTION__, webpath); path_web2os(webpath, ospath, sizeof(ospath)); WEBLOG_INFO("<%s> ospath=\"%s\".\n", __FUNCTION__, ospath); CacheWriteString(cache, "<html><head><title>Folder View</title></head><body>\n" "<style type=\"text/css\"> li {list-style: none;}</style> " "<h1>Index of "); CacheWriteString(cache, webpath); CacheWriteString(cache, "</h1>\n<ul>\n"); CacheWriteString(cache, "<li><a href=\""); /* calculate parent folder */ strcpy(strbuf, webpath); if(0 != strlen(strbuf)) { p = strrchr(strbuf, '/'); if(p && p != strbuf) { *p = 0; CacheWriteString(cache, strbuf); WEBLOG_INFO("<%s> parent=\"%s\".\n", __FUNCTION__, strbuf); } } CacheWriteString(cache, "/"); CacheWriteString(cache, "\"> Parent Directory</a></li>\n"); isdir = web_isfolder(webpath); WEBLOG_INFO("<%s> \"%s\" folder? %d\n", __FUNCTION__, webpath, isdir); if(!isdir) { strcpy(subwebpath, webpath); goto __magic_start; } if ((dir = web_opendir (webpath)) != NULL) { WEBLOG_INFO("<%s> list %s\n", __FUNCTION__, ospath); /* list all the files and sub directories */ while ((ent = readdir (dir)) != NULL) { if(dot_dir(ent->d_name)) continue; if(TRUE == endwith(ospath, "/")) sprintf(strbuf, "%s%s", ospath, ent->d_name); else sprintf(strbuf, "%s/%s", ospath, ent->d_name); path_os2web(strbuf, subwebpath, sizeof(subwebpath)); WEBLOG_INFO("<%s> subwebpath %s\n", __FUNCTION__, subwebpath); __magic_start: if(web_isfolder(subwebpath)) CacheWriteString(cache,"<li> + "); else CacheWriteString(cache,"<li> "); p = subwebpath+strlen(webpath); if('/' == *p) p++; WEBLOG_INFO("<%s> p %s\n", __FUNCTION__, p); CacheWriteString(cache, "<a href=\""); memset(enc_url, 0, sizeof(enc_url)); url_encode(subwebpath, enc_url); CacheWriteString(cache, enc_url); CacheWriteString(cache, "\">"); CacheWriteString(cache, p); CacheWriteString(cache, "</a></li>\n"); if(!isdir) goto __magic_end; } closedir(dir); } else { WEBLOG_ERROR("<%s> could not open directory %s\n", __FUNCTION__, ospath); goto __magic_end; } __magic_end: CacheWriteString(cache,"</ul>\n</body></html>"); return OK; }