std::string parse(const std::string& xml, const std::string& name, const std::string& url, const std::string& html, const std::string& htmlheader, std::vector<std::pair<std::string, std::string> >& attaches) { std::string ret(""); std::string pp = getParserPath(url); xsltStylesheetPtr xslt = xsltParseStylesheetFile(BAD_CAST pp.c_str()); htmlDocPtr doc = NULL; static std::string encoding("gb18030"); std::string mimetype = getMIMEType(htmlheader, html); if (!mimetype.empty() && mimetype == "text/xml") { doc = html.empty() ? NULL : xmlReadDoc(BAD_CAST html.c_str(), NULL, encoding.c_str(), XML_PARSE_RECOVER); } else { doc = html.empty() ? NULL : htmlParseDoc(BAD_CAST html.c_str(), encoding.c_str()); } if (doc != NULL) { const char *params[7] = {0}; size_t n_param = 0; params[n_param] = NULL; xmlDocPtr res = xsltApplyStylesheet(xslt, doc, params); //free_xslt_params(params, n_param); if (res != NULL) { xmlChar *s = NULL; int len = 0; if (xsltSaveResultToString(&s, &len, res, xslt) >= 0) { ret.assign((const char *)s, len); xmlFree(s); } xmlFreeDoc(res); } xmlFreeDoc(doc); } return ret; }
//-------------------------------------------------------------------------- int HTTPDServer::page (struct MHD_Connection *connection, const char * page) { int ret = 0; // char * root = getenv("FAUSTDocumentRoot"); string file = "."; file += page; const char* type = getMIMEType (file); int fd; #if defined(_WIN32) && !defined(__MINGW32__) int fhd; fd = _sopen_s(&fhd, file.c_str(), _O_RDONLY, _SH_DENYNO, _S_IREAD); #else fd = open (file.c_str(), O_RDONLY); #endif if (fd != -1) { int length = lseek(fd, (long)0, SEEK_END); lseek(fd, 0, SEEK_SET); struct MHD_Response *response = MHD_create_response_from_fd (length, fd); if (!response ) { cerr << "MHD_create_response_from_fd error: null response\n"; return MHD_NO; } MHD_add_response_header (response, "Content-Type", type ? type : "text/plain"); MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); } else { ret = send (connection, "", 0, MHD_HTTP_NOT_FOUND); } return ret; }