1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Get a web page, extract the title with libxml.
26 // Written by Lars Nilsson
28 // GNU C++ compile command line suggestion (edit paths accordingly):
30 // g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \
31 // -o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2
37 #include <curl/curl.h>
38 #include <libxml/HTMLparser.h>
41 // Case-insensitive string comparison
45 #define COMPARE(a, b) (!_stricmp((a), (b)))
47 #define COMPARE(a, b) (!strcasecmp((a), (b)))
51 // libxml callback context structure
56 Context(): addTitle(false) { }
63 // libcurl variables for error strings and returned data
65 static char errorBuffer[CURL_ERROR_SIZE];
66 static std::string buffer;
69 // libcurl write callback function
72 static int writer(char *data, size_t size, size_t nmemb,
73 std::string *writerData)
75 if (writerData == NULL)
78 writerData->append(data, size*nmemb);
84 // libcurl connection initialization
87 static bool init(CURL *&conn, char *url)
91 conn = curl_easy_init();
95 fprintf(stderr, "Failed to create CURL connection\n");
100 code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer);
101 if (code != CURLE_OK)
103 fprintf(stderr, "Failed to set error buffer [%d]\n", code);
108 code = curl_easy_setopt(conn, CURLOPT_URL, url);
109 if (code != CURLE_OK)
111 fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
116 code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1L);
117 if (code != CURLE_OK)
119 fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
124 code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
125 if (code != CURLE_OK)
127 fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
132 code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, &buffer);
133 if (code != CURLE_OK)
135 fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
144 // libxml start element callback function
147 static void StartElement(void *voidContext,
149 const xmlChar **attributes)
151 Context *context = (Context *)voidContext;
153 if (COMPARE((char *)name, "TITLE"))
156 context->addTitle = true;
162 // libxml end element callback function
165 static void EndElement(void *voidContext,
168 Context *context = (Context *)voidContext;
170 if (COMPARE((char *)name, "TITLE"))
171 context->addTitle = false;
175 // Text handling helper function
178 static void handleCharacters(Context *context,
179 const xmlChar *chars,
182 if (context->addTitle)
183 context->title.append((char *)chars, length);
187 // libxml PCDATA callback function
190 static void Characters(void *voidContext,
191 const xmlChar *chars,
194 Context *context = (Context *)voidContext;
196 handleCharacters(context, chars, length);
200 // libxml CDATA callback function
203 static void cdata(void *voidContext,
204 const xmlChar *chars,
207 Context *context = (Context *)voidContext;
209 handleCharacters(context, chars, length);
213 // libxml SAX callback structure
216 static htmlSAXHandler saxHandler =
248 // Parse given (assumed to be) HTML text and return the title
251 static void parseHtml(const std::string &html,
254 htmlParserCtxtPtr ctxt;
257 ctxt = htmlCreatePushParserCtxt(&saxHandler, &context, "", 0, "",
258 XML_CHAR_ENCODING_NONE);
260 htmlParseChunk(ctxt, html.c_str(), html.size(), 0);
261 htmlParseChunk(ctxt, "", 0, 1);
263 htmlFreeParserCtxt(ctxt);
265 title = context.title;
268 int main(int argc, char *argv[])
274 // Ensure one argument is given
278 fprintf(stderr, "Usage: %s <url>\n", argv[0]);
283 curl_global_init(CURL_GLOBAL_DEFAULT);
285 // Initialize CURL connection
287 if (!init(conn, argv[1]))
289 fprintf(stderr, "Connection initializion failed\n");
294 // Retrieve content for the URL
296 code = curl_easy_perform(conn);
297 curl_easy_cleanup(conn);
299 if (code != CURLE_OK)
301 fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
306 // Parse the (assumed) HTML code
308 parseHtml(buffer, title);
310 // Display the extracted title
312 printf("Title: %s\n", title.c_str());