gboolean ide_xml_sax_parse (IdeXmlSax *self, const gchar *data, gsize length, const gchar *uri, gpointer user_data) { gboolean wellformed; g_return_val_if_fail (IDE_IS_XML_SAX (self), FALSE); g_return_val_if_fail (data != NULL, FALSE); g_return_val_if_fail (length > 0, FALSE); g_return_val_if_fail (self->initialized == TRUE, FALSE); g_return_val_if_fail (self->context == NULL, FALSE); self->context = xmlCreateMemoryParserCtxt (data, length); self->context->userData = user_data; self->context->sax = &self->handler; self->handler.initialized = XML_SAX2_MAGIC; xmlCtxtUseOptions (self->context, XML_PARSE_RECOVER | XML_PARSE_NOENT); xmlParseDocument (self->context); wellformed = self->context->wellFormed; self->context->sax = NULL; g_clear_pointer (&self->context, xmlFreeParserCtxt); return wellformed; }
/* create a new CMML parser */ GstCmmlParser * gst_cmml_parser_new (GstCmmlParserMode mode) { GstCmmlParser *parser = g_malloc (sizeof (GstCmmlParser)); parser->mode = mode; parser->context = xmlCreatePushParserCtxt (NULL, NULL, NULL, 0, "cmml-bitstream"); xmlCtxtUseOptions (parser->context, XML_PARSE_NONET | XML_PARSE_NOERROR); parser->context->_private = parser; parser->context->sax->startElementNs = (startElementNsSAX2Func) gst_cmml_parser_parse_start_element_ns; parser->context->sax->endElementNs = (endElementNsSAX2Func) gst_cmml_parser_parse_end_element_ns; parser->context->sax->processingInstruction = (processingInstructionSAXFunc) gst_cmml_parser_parse_processing_instruction; parser->preamble_callback = NULL; parser->cmml_end_callback = NULL; parser->stream_callback = NULL; parser->head_callback = NULL; parser->clip_callback = NULL; parser->user_data = NULL; return parser; }
PHP_XML_API XML_Parser XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *memsuite, const XML_Char *sep) { XML_Parser parser; parser = (XML_Parser) emalloc(sizeof(struct _XML_Parser)); memset(parser, 0, sizeof(struct _XML_Parser)); parser->use_namespace = 0; parser->_ns_separator = NULL; parser->parser = xmlCreatePushParserCtxt((xmlSAXHandlerPtr) &php_xml_compat_handlers, (void *) parser, NULL, 0, NULL); if (parser->parser == NULL) { efree(parser); return NULL; } xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX); parser->parser->replaceEntities = 1; parser->parser->wellFormed = 0; if (sep != NULL) { parser->use_namespace = 1; parser->parser->sax2 = 1; parser->_ns_separator = xmlStrdup(sep); } else { /* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt so must be set in the handlers */ parser->parser->sax->initialized = 1; } return parser; }
ret_t cherokee_handler_tmi_init (cherokee_handler_tmi_t *hdl) { ret_t ret; cherokee_connection_t *conn = HANDLER_CONN(hdl); cherokee_buffer_t *tmp = &HANDLER_THREAD(hdl)->tmp_buf1; cherokee_handler_tmi_props_t *props = HANDLER_TMI_PROPS(hdl); /* We are going to look for gzipped encoding */ cherokee_buffer_clean (tmp); ret = cherokee_header_copy_known (&conn->header, header_content_encoding, tmp); if (ret == ret_ok && cherokee_buffer_cmp_str(tmp, "gzip") == 0) { TRACE(ENTRIES, "ZeroMQ: incomming header '%s'\n", tmp->buf); hdl->inflated = true; } else { cherokee_buffer_clean (tmp); ret = cherokee_header_copy_known (&conn->header, header_content_type, tmp); if (ret == ret_ok && (cherokee_buffer_cmp_str(tmp, "application/gzip") == 0 || cherokee_buffer_cmp_str(tmp, "application/zip") == 0)) { TRACE(ENTRIES, "ZeroMQ: incomming header '%s'\n", tmp->buf); hdl->inflated = true; } else { hdl->inflated = false; } } #ifdef LIBXML_PUSH_ENABLED if (props->validate_xml) { hdl->validate_xml = true; hdl->ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL); xmlCtxtUseOptions(hdl->ctxt, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET | XML_PARSE_COMPACT); if (hdl->inflated) { /* allocate inflate state */ hdl->strm.zalloc = Z_NULL; hdl->strm.zfree = Z_NULL; hdl->strm.opaque = Z_NULL; hdl->strm.avail_in = 0; hdl->strm.next_in = Z_NULL; hdl->z_ret = inflateInit2(&(hdl->strm), 16+MAX_WBITS); if (hdl->z_ret != Z_OK) hdl->validate_xml = false; } } #endif if (!hdl->inflated) { /* If we end up here that means content is plain, lets set up an encoder */ ret = props->encoder_props->instance_func((void **)&hdl->encoder, props->encoder_props); if (unlikely (ret != ret_ok)) { return ret_error; } ret = cherokee_encoder_init (hdl->encoder, conn); if (unlikely (ret != ret_ok)) { return ret_error; } } return ret_ok; }
xmlDocPtr _waitForAnswer(int sockfd) { xmlParserCtxtPtr requestParserCtxt; xmlDocPtr parsedDoc = NULL; int resRead = 0; int bytesRead = 0; int readTries = 0; int chunkParseResult = 0; char buffer[XMLBUS_CHUNK_READLENGTH + 1]; resRead = recv(sockfd, buffer, XMLBUS_CHUNK_INITIAL_READLENGTH, 0); if (resRead > 0) { buffer[resRead] = '\0'; requestParserCtxt = xmlCreatePushParserCtxt(NULL, NULL, buffer, resRead, NULL); bytesRead += resRead; } else { printf("Read failed socket readerror:%d -> closing clientsocket", resRead); return NULL; } xmlCtxtUseOptions(requestParserCtxt,XML_PARSE_NOWARNING); while(1) { resRead = recv(sockfd, buffer, XMLBUS_CHUNK_READLENGTH, 0); buffer[resRead] = '\0'; if (resRead > 0) { chunkParseResult = xmlParseChunk(requestParserCtxt,buffer,resRead,0); if (chunkParseResult != 0) { xmlErrorPtr xmlErr = xmlGetLastError(); printf("request parsing found error %d: %s", chunkParseResult, xmlErr->message); //@TODO recover or bail out } if (requestParserCtxt->instate == XML_PARSER_EPILOG) { break; } bytesRead += resRead; continue; // don't need to parse it another time... it is done already } // end of reading (no data received) else { // Closed connection or error (in this case there is no buffer content, is it ?) if (readTries < 5) { readTries++; continue; } else { // end of parsing (check if the xml is valid) if (bytesRead > 0) { // end of reading, try to parse the message so far break; } //log4c_category_error(loggerCategory, "thread %d read: data read failed, bailing out", threadId); //close(request->clientSocket); return NULL; } } // end resRead > 0 .. else } // end while(1) chunkParseResult = xmlParseChunk(requestParserCtxt, NULL, 0, 1); parsedDoc = requestParserCtxt->myDoc; xmlFreeParserCtxt(requestParserCtxt); return parsedDoc; }
/* Taken from libxml2, xmlSAXParseMemoryWithData */ xmlDocPtr mxslt_doc_xml_load_entity(mxslt_doc_t * document, char * localfile) { xmlParserCtxtPtr ctx; xmlParserInputPtr input; xmlDocPtr retval; xmlChar * filename; mxslt_doc_debug_print(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE0, "load_entity/xmlCreateMemoryParserCtxt -- replacing entities: %08x\n", xmlSubstituteEntitiesDefaultValue); /* SNIPPET: This is a good mix&shake of * xmlCreateMemoryParserCtxt, xmlCreateFileParserCtxt */ ctx=xmlNewParserCtxt(); if(ctx == NULL) return NULL; #if LIBXML_VERSION >= 20600 xmlCtxtUseOptions(ctx, MXSLT_XSLT_OPTIONS); #endif /* Remember which document we are parsing * in this context */ /* ctx->_private=document; */ filename=xmlCanonicPath((xmlChar *)localfile); if(filename == NULL) { xmlFreeParserCtxt(ctx); return NULL; } input=xmlLoadExternalEntity((char *)filename, NULL, ctx); xmlFree(filename); if(input == NULL) { xmlFreeParserCtxt(ctx); return NULL; } inputPush(ctx, input); if(ctx->directory == NULL) ctx->directory=xmlParserGetDirectory(localfile); /* END SNIPPET */ /* MXSLT_DUMP_CTX(ctx); */ /* Parse document */ xmlParseDocument(ctx); if(ctx->wellFormed) retval=ctx->myDoc; else { retval=NULL; xmlFreeDoc(ctx->myDoc); ctx->myDoc=NULL; } xmlFreeParserCtxt(ctx); return retval; }
VALUE ruby_xml_parser_context_options_set(VALUE self, VALUE options) { ruby_xml_parser_context *rxpc; Data_Get_Struct(self, ruby_xml_parser_context, rxpc); if (xmlCtxtUseOptions(rxpc->ctxt, NUM2INT(options))) return Qfalse; else return Qtrue; }
static VALUE set_options(VALUE self, VALUE options) { xmlParserCtxtPtr ctx; Data_Get_Struct(self, xmlParserCtxt, ctx); if (xmlCtxtUseOptions(ctx, (int)NUM2INT(options)) != 0) rb_raise(rb_eRuntimeError, "Cannot set XML parser context options"); return Qnil; }
/* * call-seq: * context.options = XML::Parser::Options::NOENT | XML::Parser::Options::NOCDATA * * Provides control over the execution of a parser. Valid values * are the constants defined on XML::Parser::Options. Multiple * options can be combined by using Bitwise OR (|). */ static VALUE rxml_parser_context_options_set(VALUE self, VALUE options) { int result; xmlParserCtxtPtr ctxt; Check_Type(options, T_FIXNUM); Data_Get_Struct(self, xmlParserCtxt, ctxt); result = xmlCtxtUseOptions(ctxt, NUM2INT(options)); return self; }
/* * Read xmlDocument from server */ static xmlDoc * js_document_read (xmlParserCtxtPtr ctxt, js_session_t *jsp, const char *url, const char *encoding, int options) { xmlParserInputBufferPtr input; xmlParserInputPtr stream; xmlDoc *docp; if (jsp == NULL || ctxt == NULL || jsp->js_state == JSS_DEAD) return NULL; xmlCtxtReset(ctxt); input = js_buffer_create(jsp, XML_CHAR_ENCODING_NONE); if (input == NULL) return NULL; input->closecallback = NULL; stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); if (stream == NULL) { xmlFreeParserInputBuffer(input); return NULL; } inputPush(ctxt, stream); xmlCtxtUseOptions(ctxt, options); xmlCharEncodingHandlerPtr hdlr; if (encoding && ((hdlr = xmlFindCharEncodingHandler(encoding)) != NULL)) xmlSwitchToEncoding(ctxt, hdlr); if (url != NULL && ctxt->input != NULL && ctxt->input->filename == NULL) ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) url); /* * All right. The stage is set, time to open the curtain and let * the show begin. */ xmlParseDocument(ctxt); docp = ctxt->myDoc; ctxt->myDoc = NULL; if (docp && !ctxt->wellFormed) { xmlFreeDoc(docp); docp = NULL; } return docp; }
/** * raptor_sax2_parse_chunk: * @sax2: sax2 object * @buffer: input buffer * @len: input buffer lenght * @is_end: non-0 if end of data * * Parse a chunk of XML data generating SAX2 events * * Return value: non-0 on failure */ int raptor_sax2_parse_chunk(raptor_sax2* sax2, const unsigned char *buffer, size_t len, int is_end) { #ifdef RAPTOR_XML_LIBXML /* parser context */ xmlParserCtxtPtr xc = sax2->xc; int rc; if(!xc) { int libxml_options = 0; if(!len) { /* no data given at all */ raptor_sax2_update_document_locator(sax2, sax2->locator); raptor_log_error(sax2->world, RAPTOR_LOG_LEVEL_ERROR, sax2->locator, "XML Parsing failed - no element found"); return 1; } xc = xmlCreatePushParserCtxt(&sax2->sax, sax2, /* user data */ (char*)buffer, RAPTOR_BAD_CAST(int, len), NULL); if(!xc) goto handle_error; #ifdef RAPTOR_LIBXML_XML_PARSE_NONET if(RAPTOR_OPTIONS_GET_NUMERIC(sax2, RAPTOR_OPTION_NO_NET)) libxml_options |= XML_PARSE_NONET; #endif #ifdef HAVE_XMLCTXTUSEOPTIONS xmlCtxtUseOptions(xc, libxml_options); #endif xc->userData = sax2; /* user data */ xc->vctxt.userData = sax2; /* user data */ xc->vctxt.error = (xmlValidityErrorFunc)raptor_libxml_validation_error; xc->vctxt.warning = (xmlValidityWarningFunc)raptor_libxml_validation_warning; xc->replaceEntities = 1; sax2->xc = xc; if(is_end) len = 0; else return 0; }
LibXMLParser::LibXMLParser(XMLParserClient* client) : XMLParser(client), p(new Private()) { // Initialize libXML for multithreaded applications if (!initialized) { xmlInitParser(); initialized = true; } memset(&p->handler_, 0, sizeof(p->handler_) ); p->handler_.initialized = XML_SAX2_MAGIC; p->handler_.startElementNs = &handleStartElement; p->handler_.endElementNs = &handleEndElement; p->handler_.characters = &handleCharacterData; p->handler_.warning = &handleWarning; p->handler_.error = &handleError; p->context_ = xmlCreatePushParserCtxt(&p->handler_, this, nullptr, 0, nullptr); xmlCtxtUseOptions(p->context_, XML_PARSE_NOENT); assert(p->context_); }
static ngx_int_t ngx_http_xslt_add_chunk(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx, ngx_buf_t *b) { int err; xmlParserCtxtPtr ctxt; if (ctx->ctxt == NULL) { ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL); if (ctxt == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "xmlCreatePushParserCtxt() failed"); return NGX_ERROR; } xmlCtxtUseOptions(ctxt, XML_PARSE_NOENT|XML_PARSE_DTDLOAD |XML_PARSE_NOWARNING); ctxt->sax->externalSubset = ngx_http_xslt_sax_external_subset; ctxt->sax->setDocumentLocator = NULL; ctxt->sax->error = ngx_http_xslt_sax_error; ctxt->sax->fatalError = ngx_http_xslt_sax_error; ctxt->sax->_private = ctx; ctx->ctxt = ctxt; ctx->request = r; } err = xmlParseChunk(ctx->ctxt, (char *) b->pos, (int) (b->last - b->pos), (b->last_buf) || (b->last_in_chain)); if (err == 0) { b->pos = b->last; return NGX_OK; } ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "xmlParseChunk() failed, error:%d", err); return NGX_ERROR; }
static int create_parser(saxctxt *ctxt, const char **bufp, apr_size_t *bytes) { xmlCharEncoding enc; #ifndef USE_OLD_LIBXML2 int xmlopts = XML_PARSE_RECOVER | XML_PARSE_NONET | XML_PARSE_NOBLANKS | XML_PARSE_NOERROR | XML_PARSE_NOWARNING; #endif enc = sniff_encoding(ctxt, *bufp, *bytes); ctxt->parser = htmlCreatePushParserCtxt(&sax, ctxt, *bufp, 0, 0, enc); if (ctxt->parser == NULL) return 0; apr_pool_cleanup_register (ctxt->f->r->pool, ctxt->parser, (int(*)(void*))htmlFreeParserCtxt, apr_pool_cleanup_null); #ifndef USE_OLD_LIBXML2 if (xmlopts = xmlCtxtUseOptions(ctxt->parser, xmlopts), xmlopts) ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, ctxt->f->r, "create_parser: unsupported parser opts %x", xmlopts); #endif return 1; }
PHPAPI XML_Parser XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *memsuite, const XML_Char *sep) { XML_Parser parser; parser = (XML_Parser) emalloc(sizeof(struct _XML_Parser)); memset(parser, 0, sizeof(struct _XML_Parser)); parser->use_namespace = 0; parser->_ns_seperator = NULL; parser->parser = xmlCreatePushParserCtxt((xmlSAXHandlerPtr) &php_xml_compat_handlers, (void *) parser, NULL, 0, NULL); if (parser->parser == NULL) { efree(parser); return NULL; } #if LIBXML_VERSION <= 20617 /* for older versions of libxml2, allow correct detection of * charset in documents with a BOM: */ parser->parser->charset = XML_CHAR_ENCODING_NONE; #endif #if LIBXML_VERSION >= 20703 xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX); #endif parser->parser->replaceEntities = 1; parser->parser->wellFormed = 0; if (sep != NULL) { parser->use_namespace = 1; parser->parser->sax2 = 1; parser->_ns_seperator = xmlStrdup(sep); } else { /* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt so must be set in the handlers */ parser->parser->sax->initialized = 1; } return parser; }
/** * raptor_sax2_parse_chunk: * @sax2: sax2 object * @buffer: input buffer * @len: input buffer lenght * @is_end: non-0 if end of data * * Parse a chunk of XML data generating SAX2 events * * Return value: non-0 on failure */ int raptor_sax2_parse_chunk(raptor_sax2* sax2, const unsigned char *buffer, size_t len, int is_end) { #ifdef RAPTOR_XML_EXPAT XML_Parser xp=sax2->xp; int rc; #endif #ifdef RAPTOR_XML_LIBXML /* parser context */ xmlParserCtxtPtr xc=sax2->xc; int rc; #endif #ifdef RAPTOR_XML_LIBXML if(!xc) { int libxml_options = 0; if(!len) { /* no data given at all - emit a similar message to expat */ raptor_sax2_update_document_locator(sax2, sax2->locator); raptor_log_error_to_handlers(sax2->world, sax2->error_handlers, RAPTOR_LOG_LEVEL_ERROR, sax2->locator, "XML Parsing failed - no element found"); return 1; } xc = xmlCreatePushParserCtxt(&sax2->sax, sax2, /* user data */ (char*)buffer, len, NULL); if(!xc) goto handle_error; #ifdef RAPTOR_LIBXML_XML_PARSE_NONET if(sax2->feature_no_net) libxml_options |= XML_PARSE_NONET; #endif #ifdef HAVE_XMLCTXTUSEOPTIONS xmlCtxtUseOptions(xc, libxml_options); #endif xc->userData = sax2; /* user data */ xc->vctxt.userData = sax2; /* user data */ xc->vctxt.error=(xmlValidityErrorFunc)raptor_libxml_validation_error; xc->vctxt.warning=(xmlValidityWarningFunc)raptor_libxml_validation_warning; xc->replaceEntities = 1; sax2->xc = xc; if(is_end) len=0; else return 0; } #endif if(!len) { #ifdef RAPTOR_XML_EXPAT rc=XML_Parse(xp, (char*)buffer, 0, 1); if(!rc) /* expat: 0 is failure */ goto handle_error; #endif #ifdef RAPTOR_XML_LIBXML xmlParseChunk(xc, (char*)buffer, 0, 1); #endif return 0; } #ifdef RAPTOR_XML_EXPAT rc=XML_Parse(xp, (char*)buffer, len, is_end); if(!rc) /* expat: 0 is failure */ goto handle_error; if(is_end) return 0; #endif #ifdef RAPTOR_XML_LIBXML /* This works around some libxml versions that fail to work * if the buffer size is larger than the entire file * and thus the entire parsing is done in one operation. * * The code below: * 2.4.19 (oldest tested) to 2.4.24 - required * 2.4.25 - works with or without it * 2.4.26 or later - fails with this code */ #if LIBXML_VERSION < 20425 if(sax2->first_read && is_end) { /* parse all but the last character */ rc = xmlParseChunk(xc, (char*)buffer, len-1, 0); if(rc && rc != XML_WAR_UNDECLARED_ENTITY) goto handle_error; /* last character */ rc = xmlParseChunk(xc, (char*)buffer + (len-1), 1, 0); if(rc && rc != XML_WAR_UNDECLARED_ENTITY) goto handle_error; /* end */ xmlParseChunk(xc, (char*)buffer, 0, 1); return 0; } #endif #if LIBXML_VERSION < 20425 sax2->first_read=0; #endif rc = xmlParseChunk(xc, (char*)buffer, len, is_end); if(rc && rc != XML_WAR_UNDECLARED_ENTITY) /* libxml: non 0 is failure */ goto handle_error; if(is_end) return 0; #endif return 0; #if defined(RAPTOR_XML_EXPAT) || defined(RAPTOR_XML_LIBXML) handle_error: #endif #ifdef RAPTOR_XML_EXPAT #ifdef EXPAT_UTF8_BOM_CRASH if(sax2->tokens_count) { #endif /* Work around a bug with the expat 1.95.1 shipped with RedHat 7.2 * which dies here if the error is before <?xml?... * The expat 1.95.1 source release version works fine. */ if(sax2->locator) raptor_sax2_update_document_locator(sax2, sax2->locator); #ifdef EXPAT_UTF8_BOM_CRASH } #endif #endif /* EXPAT */ #ifdef RAPTOR_XML_EXPAT if(1) { const char *error_prefix="XML Parsing failed - "; /* 21 chars */ #define ERROR_PREFIX_LEN 21 const char *error_message=XML_ErrorString(XML_GetErrorCode(xp)); size_t error_length; char *error_buffer; error_length=strlen(error_message); error_buffer=(char*)RAPTOR_MALLOC(cstring, ERROR_PREFIX_LEN + error_length+1); if(error_buffer) { strncpy(error_buffer, error_prefix, ERROR_PREFIX_LEN); strncpy(error_buffer+ERROR_PREFIX_LEN, error_message, error_length+1); raptor_log_error_to_handlers(sax2->world, sax2->error_handlers, RAPTOR_LOG_LEVEL_ERROR, sax2->locator, error_buffer); RAPTOR_FREE(cstring, error_buffer); } else raptor_log_error_to_handlers(sax2->world, sax2->error_handlers, RAPTOR_LOG_LEVEL_ERROR, sax2->locator, "XML Parsing failed"); } #endif return 1; }
/* Taken from libxml2, xmlSAXParseMemoryWithData */ xmlDocPtr mxslt_doc_xml_parse(mxslt_doc_t * document, xmlParserInputBufferPtr buf, char * localfile) { xmlParserCtxtPtr ctx; xmlParserInputPtr input; xmlDocPtr retval; mxslt_doc_debug_print(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE0, "xml_parse/xmlCreateMemoryParserCtxt -- replacing entities: %08x\n", xmlSubstituteEntitiesDefaultValue); if(buf == NULL) return NULL; /* SNIPPET: This is a good mix&shake of * xmlCreateMemoryParserCtxt, xmlCreateFileParserCtxt */ ctx=xmlNewParserCtxt(); if(ctx == NULL) { xmlFreeParserInputBuffer(buf); return NULL; } #if LIBXML_VERSION >= 20600 xmlCtxtUseOptions(ctx, MXSLT_XSLT_OPTIONS); #endif /* Default are no longer changed... since 2.6.0 they * are completely ignored, leading to broken modxslt :|| */ if(ctx->sax) { ctx->sax->resolveEntity=mxslt_sax_resolve_entity; ctx->sax->processingInstruction=mxslt_sax_processing_instruction; } /* Remember which document we are parsing * in this context */ /* ctx->_private=document; */ input=xmlNewInputStream(ctx); if(input == NULL) { xmlFreeParserInputBuffer(buf); xmlFreeParserCtxt(ctx); return NULL; } input->filename=(char *)xmlCanonicPath((xmlChar *)localfile); if(input->filename == NULL) { xmlFreeParserCtxt(ctx); xmlFreeParserInputBuffer(buf); xmlFreeInputStream(input); return NULL; } input->buf=buf; #if LIBXML_VERSION < 20900 input->base=input->buf->buffer->content; input->cur=input->buf->buffer->content; input->end=&input->buf->buffer->content[input->buf->buffer->use]; #else /* With libxml2 2.9.0, the buffer struct can only be accessed through * methods. */ input->base=xmlBufContent(input->buf->buffer); input->cur=xmlBufContent(input->buf->buffer); input->end=xmlBufEnd(input->buf->buffer); #endif inputPush(ctx, input); if(ctx->directory == NULL) ctx->directory=xmlParserGetDirectory(localfile); /* END SNIPPET */ if(mxslt_doc_debug_level(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE2)) mxslt_doc_dump_ctx(ctx, document); /* Parse document */ xmlParseDocument(ctx); if(ctx->wellFormed) retval=ctx->myDoc; else { retval=NULL; xmlFreeDoc(ctx->myDoc); ctx->myDoc=NULL; } xmlFreeParserCtxt(ctx); /* xmlFreeParserInputBuffer(buf); */ return retval; }
void se_parser::parse_output_xml(char *output, std::vector<search_snippet*> *snippets, const int &count_offset) throw (sp_exception) { _count = count_offset; xmlParserCtxtPtr ctxt = NULL; parser_context pc; pc._parser = this; pc._snippets = snippets; pc._current_snippet = NULL; xmlSAXHandler saxHandler = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, start_element_wrapper, end_element_wrapper, NULL, characters_wrapper, NULL, NULL, NULL, NULL, NULL, NULL, NULL, cdata_wrapper, NULL, NULL, NULL, NULL, NULL, NULL }; //mutex_lock(&se_parser::_se_parser_mutex); int status = 0; try { ctxt = xmlCreatePushParserCtxt(&saxHandler, &pc, "", 0, ""); xmlCtxtUseOptions(ctxt,XML_PARSE_NOERROR); status = xmlParseChunk(ctxt,output,strlen(output),0); } catch (std::exception e) { errlog::log_error(LOG_LEVEL_PARSER,"Error %s in xml/html parsing of search results.", e.what()); //mutex_unlock(&se_parser::_se_parser_mutex); throw sp_exception(WB_ERR_PARSE,e.what()); } catch (...) // catch everything else to avoid crashes. { std::string msg = "Unknown error in xml/html parsing of search results"; errlog::log_error(LOG_LEVEL_PARSER,msg.c_str()); //mutex_unlock(&se_parser::_se_parser_mutex); throw sp_exception(WB_ERR_PARSE,msg); } if (status == 0) { if (ctxt) xmlFreeParserCtxt(ctxt); //mutex_unlock(&se_parser::_se_parser_mutex); } else // an error occurred. { xmlErrorPtr xep = xmlCtxtGetLastError(ctxt); if (xep) { std::string err_msg = std::string(xep->message); miscutil::replace_in_string(err_msg,"\n",""); errlog::log_error(LOG_LEVEL_PARSER, "html level parsing error (libxml2): %s", err_msg.c_str()); // check on error level. if (xep->level == 3) // fatal or recoverable error. { std::string msg = "libxml2 fatal error"; errlog::log_error(LOG_LEVEL_PARSER,msg.c_str()); if (ctxt) xmlFreeParserCtxt(ctxt); //mutex_unlock(&se_parser::_se_parser_mutex); throw sp_exception(WB_ERR_PARSE,msg); } // XXX: too verbose, and confusing to users. else if (xep->level == 2) { std::string msg = "libxml2 recoverable error"; errlog::log_error(LOG_LEVEL_DEBUG,msg.c_str()); if (ctxt) xmlFreeParserCtxt(ctxt); //mutex_unlock(&se_parser::_se_parser_mutex); //throw sp_exception(WB_ERR_PARSE,msg); } } } }
/******************************************************************************* * cas_cas2_servicevalidate: Perform CAS2 validation protocol */ CAS_CODE cas_cas2_servicevalidate( CAS* cas, char* cas2_servicevalidate_url, char* escaped_service, char* ticket, int renew) { if(!cas && cas2_servicevalidate_url && escaped_service && ticket) { return(CAS_INVALID_PARAMETERS); } if(cas->principal) { free(cas->principal); cas->principal=NULL; } xmlSAXHandlerPtr sax=calloc( 1,sizeof( xmlSAXHandler ) ); if(sax==NULL) return(CAS_ENOMEM); sax->initialized=XML_SAX2_MAGIC; sax->startElementNs=( startElementNsSAX2Func )cas_cas2_startElementNs; sax->endElementNs=( endElementNsSAX2Func )cas_cas2_endElementNs; sax->characters=( charactersSAXFunc )cas_cas2_characters; sax->startDocument=( startDocumentSAXFunc )cas_cas2_startDocument; sax->endDocument=( endDocumentSAXFunc )cas_cas2_endDocument; CAS_XML_STATE state= {cas,XML_NEED_START_DOC}; xmlParserCtxtPtr ctx=xmlCreatePushParserCtxt( sax, &state, NULL,0,NULL ); xmlCtxtUseOptions( ctx,XML_PARSE_NOBLANKS ); //Build URL for validation //18=strlen("?service=")+strlen("&ticket=")+1 //11=strlen("&renew=true") char* url; if(url=calloc( strlen( cas2_servicevalidate_url )+strlen( escaped_service )+strlen( ticket )+( renew?29:18 ),sizeof( char ) )) { strcpy( url,cas2_servicevalidate_url ); strcat( url,"?service=" ); strcat( url,escaped_service ); strcat( url,"&ticket=" ); strcat( url,ticket ); if(renew) strcat( url, "&renew=true"); //Setup curl connection curl_easy_setopt( cas->curl,CURLOPT_URL, url ); //Set response handler curl_easy_setopt( cas->curl,CURLOPT_WRITEFUNCTION, ( curl_write_callback )cas_cas2_curl_callback ); //Pass state to response handler curl_easy_setopt( cas->curl,CURLOPT_WRITEDATA, ctx ); int curl_status=curl_easy_perform( cas->curl ); int xmlParseError = xmlParseChunk( ctx,NULL,0,1 ); free(sax); free(url); xmlFreeParserCtxt(ctx); if(curl_status==0) { if( state.xml_state==XML_COMPLETE ) { return( cas->code ); } else if(xmlParseError) { return(CAS2_INVALID_XML); } else { return(CAS_INVALID_RESPONSE); } } else { if(cas->message) { free(cas->message); } cas->message=strdup(curl_easy_strerror(curl_status)); return(CAS_CURL_FAILURE); } } else { //ENOMEM for url calloc return(CAS_ENOMEM); } }