Apache2.4 module selbst gestrickt - Linux

jkallup

Erfahrenes Mitglied
Hallo xml'er,

habe ein Problem.
Und zwar wird in der Zeile "look here" scheinbar eine Exception
geworfen.
Jedenfalls kommt der Browser nicht über diese Zeile.
Ist da was falsch?
Ich arbeite unter Linux Debian 8.0 jessie gcc c++ 4.7 4.8

Für sachdienliche Hinweise bin ich immer froh.
Gruß

Code:
#ifdef __cplusplus
#define EXTERN_C_BLOCK_BEGIN  extern "C" {
#define EXTERN_C_BLOCK_END  }
#define EXTERN_C_FUNC  extern "C"
#else
#define EXTERN_C_BLOCK_BEGIN
#define EXTERN_C_BLOCK_END
#define EXTERN_C_FUNC
#endif

#include <libxml2/libxml/tree.h>
#include <libxml2/libxml/HTMLparser.h>
#include <libxml++/libxml++.h>

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

#include <iostream>
#include <algorithm>
#include <string>

#include <stdio.h>
#include "apr_hash.h"
#include "ap_config.h"
#include "ap_provider.h"
#include "httpd.h"
#include "http_core.h"
#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"

#define HEADER_ACCEPT "Accept:text/html,application/xhtml+xml,application/xml"
#define HEADER_USER_AGENT "User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17"

EXTERN_C_FUNC int test_html(request_rec *req, char*);
EXTERN_C_FUNC
int test_html(request_rec *req)  // apache2.4 module: external call to there
{
  std::string url = "http://127.0.0.1/";
  curlpp::Easy request;

  // Specify the URL
  request.setOpt(curlpp::options::Url(url));

  // Specify some headers
  std::list<std::string> headers;
  headers.push_back(HEADER_ACCEPT);
  headers.push_back(HEADER_USER_AGENT);
  request.setOpt(new curlpp::options::HttpHeader(headers));
  request.setOpt(new curlpp::options::FollowLocation(true));

  // Configure curlpp to use stream
  std::ostringstream responseStream;
  curlpp::options::WriteStream streamWriter(&responseStream);
  request.setOpt(streamWriter);

  // Collect response
  request.perform();
  std::string re = responseStream.str();

  // Parse HTML and create a DOM tree
  xmlDoc* doc = htmlReadDoc((xmlChar*)re.c_str(), NULL, NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);

  // Encapsulate raw libxml document in a libxml++ wrapper
  xmlNode* r = xmlDocGetRootElement(doc);
  xmlpp::Element* root = new xmlpp::Element(r);

  std::string xpath = "/html/div";
  auto elements = root->find(xpath);

  // the line below is the crash point  <--- "look here"
  std::string dstr = dynamic_cast<xmlpp::ContentNode*>(elements[0])->get_content();
  return OK;

  ap_rprintf(req,dstr.c_str());
  xmlFreeDoc(doc);

  return OK;
}
 
Hallo,

Dir auch noch ein Gutes Neues!
Ich kann das nicht so einfach Debuggen, da ich das im Browser aufrufe.
Wie gekennzeichnet, in Zeile 75.
 
Du verwendest dynamic_cast mit rohen Zeigern und fragst warum da was schief geht?
Wieviel Erfahrung hast du? Das kann nicht viel sein.
Oder deutlicher: sowas macht kein vernünftiger Entwickler, dynamic_cast verschleiert nur laienhaftes Programmdesign und in Kombination mit rohen C-Zeigern wird sowas gar nicht verwendet.
 
Hallo,

habe soeben des Rätzel's Lösung gefunden:

Code:
  std::string xpath = "//html/head/title/text()";
  auto elements = root->find(xpath);

der XPath war falsch definiert/gesetzt - text()
Nun suche ich noch nach nen XPath Manual - aber das gibt sicherlich google noch bekannt :)

Gruß
 
Zurück