2011-03-12 00:14:32 +01:00
|
|
|
/***************************************************************************
|
2005-01-31 18:22:40 +00:00
|
|
|
* _ _ ____ _
|
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
|
*
|
2023-01-02 13:51:48 +01:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-12 00:14:32 +01:00
|
|
|
*
|
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 14:02:01 +01:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-12 00:14:32 +01:00
|
|
|
*
|
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
|
*
|
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
|
* KIND, either express or implied.
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 11:16:50 +02:00
|
|
|
*
|
2011-03-12 00:14:32 +01:00
|
|
|
***************************************************************************/
|
2015-07-01 11:43:12 +02:00
|
|
|
/* <DESC>
|
2025-11-14 17:55:33 +01:00
|
|
|
* Get a webpage, extract the title with libxml.
|
2015-07-01 11:43:12 +02:00
|
|
|
* </DESC>
|
2026-06-28 22:38:51 +02:00
|
|
|
*
|
|
|
|
|
* Written by Lars Nilsson
|
|
|
|
|
*
|
|
|
|
|
* GNU C++ compile command line suggestion (edit paths accordingly):
|
|
|
|
|
*
|
|
|
|
|
* g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \
|
|
|
|
|
* -o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2
|
|
|
|
|
*/
|
2005-01-31 18:22:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string>
|
2025-11-17 23:48:39 +01:00
|
|
|
|
2005-01-31 18:22:40 +00:00
|
|
|
#include <curl/curl.h>
|
2025-11-17 23:48:39 +01:00
|
|
|
|
2005-01-31 18:22:40 +00:00
|
|
|
#include <libxml/HTMLparser.h>
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// Case-insensitive string comparison
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
|
|
|
|
|
2024-12-20 02:00:22 +01:00
|
|
|
#ifdef _WIN32
|
2025-12-07 16:49:55 +01:00
|
|
|
#define COMPARE(a, b) (!_stricmp(a, b))
|
2005-01-31 18:22:40 +00:00
|
|
|
#else
|
2025-12-07 16:49:55 +01:00
|
|
|
#define COMPARE(a, b) (!strcasecmp(a, b))
|
2005-01-31 18:22:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libxml callback context structure
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
2025-11-28 14:19:18 +01:00
|
|
|
struct Context {
|
|
|
|
|
Context() : addTitle(false) {}
|
2005-01-31 18:22:40 +00:00
|
|
|
|
|
|
|
|
bool addTitle;
|
|
|
|
|
std::string title;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libcurl variables for error strings and returned data
|
2005-01-31 18:22:40 +00:00
|
|
|
|
|
|
|
|
static char errorBuffer[CURL_ERROR_SIZE];
|
|
|
|
|
static std::string buffer;
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libcurl write callback function
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
2024-12-20 02:00:22 +01:00
|
|
|
static size_t writer(char *data, size_t size, size_t nmemb,
|
|
|
|
|
std::string *writerData)
|
2005-01-31 18:22:40 +00:00
|
|
|
{
|
2026-06-09 14:18:02 +02:00
|
|
|
if(!writerData)
|
2005-01-31 18:22:40 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2025-11-28 14:19:18 +01:00
|
|
|
writerData->append(data, size * nmemb);
|
2005-01-31 18:22:40 +00:00
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libcurl connection initialization
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
2025-10-31 14:42:30 +01:00
|
|
|
static bool init(CURL *&curl, const char *url)
|
2005-01-31 18:22:40 +00:00
|
|
|
{
|
2025-12-16 15:54:06 +01:00
|
|
|
CURLcode result;
|
2005-01-31 18:22:40 +00:00
|
|
|
|
2025-10-31 14:42:30 +01:00
|
|
|
curl = curl_easy_init();
|
2005-01-31 18:22:40 +00:00
|
|
|
|
2025-10-31 14:42:30 +01:00
|
|
|
if(!curl) {
|
|
|
|
|
fprintf(stderr, "Failed to create CURL handle\n");
|
2025-02-27 11:32:43 +01:00
|
|
|
return false;
|
2005-01-31 18:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
result = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
|
|
|
|
|
if(result != CURLE_OK) {
|
|
|
|
|
fprintf(stderr, "Failed to set error buffer [%d]\n", result);
|
2005-01-31 18:22:40 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
result = curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
|
|
|
if(result != CURLE_OK) {
|
2005-01-31 18:22:40 +00:00
|
|
|
fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
result = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
|
if(result != CURLE_OK) {
|
2005-01-31 18:22:40 +00:00
|
|
|
fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
result = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
|
|
|
|
|
if(result != CURLE_OK) {
|
2005-01-31 18:22:40 +00:00
|
|
|
fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
result = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
|
|
|
|
if(result != CURLE_OK) {
|
2005-01-31 18:22:40 +00:00
|
|
|
fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libxml start element callback function
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
2005-02-09 15:15:01 +00:00
|
|
|
static void StartElement(void *voidContext,
|
2005-01-31 18:22:40 +00:00
|
|
|
const xmlChar *name,
|
|
|
|
|
const xmlChar **attributes)
|
|
|
|
|
{
|
2019-05-25 19:36:35 +02:00
|
|
|
Context *context = static_cast<Context *>(voidContext);
|
2005-01-31 18:22:40 +00:00
|
|
|
|
2024-12-20 02:00:22 +01:00
|
|
|
if(COMPARE(reinterpret_cast<const char *>(name), "TITLE")) {
|
2005-01-31 18:22:40 +00:00
|
|
|
context->title = "";
|
|
|
|
|
context->addTitle = true;
|
|
|
|
|
}
|
2025-07-14 17:08:05 +02:00
|
|
|
(void)attributes;
|
2005-01-31 18:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libxml end element callback function
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
2005-02-09 15:15:01 +00:00
|
|
|
static void EndElement(void *voidContext,
|
2005-01-31 18:22:40 +00:00
|
|
|
const xmlChar *name)
|
|
|
|
|
{
|
2019-05-25 19:36:35 +02:00
|
|
|
Context *context = static_cast<Context *>(voidContext);
|
2005-01-31 18:22:40 +00:00
|
|
|
|
2024-12-20 02:00:22 +01:00
|
|
|
if(COMPARE(reinterpret_cast<const char *>(name), "TITLE"))
|
2005-01-31 18:22:40 +00:00
|
|
|
context->addTitle = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// Text handling helper function
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
|
|
|
|
static void handleCharacters(Context *context,
|
|
|
|
|
const xmlChar *chars,
|
|
|
|
|
int length)
|
|
|
|
|
{
|
2017-01-08 16:38:54 +01:00
|
|
|
if(context->addTitle)
|
2024-12-20 02:00:22 +01:00
|
|
|
context->title.append(reinterpret_cast<const char *>(chars),
|
|
|
|
|
(unsigned long)length);
|
2005-01-31 18:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libxml PCDATA callback function
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
2005-02-09 15:15:01 +00:00
|
|
|
static void Characters(void *voidContext,
|
2005-01-31 18:22:40 +00:00
|
|
|
const xmlChar *chars,
|
|
|
|
|
int length)
|
|
|
|
|
{
|
2019-05-25 19:36:35 +02:00
|
|
|
Context *context = static_cast<Context *>(voidContext);
|
2005-01-31 18:22:40 +00:00
|
|
|
|
|
|
|
|
handleCharacters(context, chars, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libxml CDATA callback function
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
|
|
|
|
static void cdata(void *voidContext,
|
|
|
|
|
const xmlChar *chars,
|
|
|
|
|
int length)
|
|
|
|
|
{
|
2019-05-25 19:36:35 +02:00
|
|
|
Context *context = static_cast<Context *>(voidContext);
|
2005-01-31 18:22:40 +00:00
|
|
|
|
|
|
|
|
handleCharacters(context, chars, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// libxml SAX callback structure
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
2025-11-29 23:11:52 +01:00
|
|
|
static htmlSAXHandler saxHandler = {
|
2005-01-31 18:22:40 +00:00
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
2005-02-09 15:15:01 +00:00
|
|
|
StartElement,
|
|
|
|
|
EndElement,
|
2005-01-31 18:22:40 +00:00
|
|
|
NULL,
|
2005-02-09 15:15:01 +00:00
|
|
|
Characters,
|
2005-01-31 18:22:40 +00:00
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
cdata,
|
2024-12-20 02:00:22 +01:00
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
2005-01-31 18:22:40 +00:00
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//
|
2026-01-02 01:34:05 +01:00
|
|
|
// Parse given (assumed to be) HTML text and return the title
|
2005-01-31 18:22:40 +00:00
|
|
|
//
|
|
|
|
|
static void parseHtml(const std::string &html,
|
|
|
|
|
std::string &title)
|
|
|
|
|
{
|
|
|
|
|
htmlParserCtxtPtr ctxt;
|
|
|
|
|
Context context;
|
|
|
|
|
|
|
|
|
|
ctxt = htmlCreatePushParserCtxt(&saxHandler, &context, "", 0, "",
|
|
|
|
|
XML_CHAR_ENCODING_NONE);
|
|
|
|
|
|
2024-12-20 02:00:22 +01:00
|
|
|
htmlParseChunk(ctxt, html.c_str(), (int)html.size(), 0);
|
2005-01-31 18:22:40 +00:00
|
|
|
htmlParseChunk(ctxt, "", 0, 1);
|
|
|
|
|
|
|
|
|
|
htmlFreeParserCtxt(ctxt);
|
|
|
|
|
|
|
|
|
|
title = context.title;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-01 03:57:45 +01:00
|
|
|
int main(int argc, const char *argv[])
|
2005-01-31 18:22:40 +00:00
|
|
|
{
|
2025-10-31 14:42:30 +01:00
|
|
|
CURL *curl = NULL;
|
2025-12-16 15:54:06 +01:00
|
|
|
CURLcode result;
|
2005-01-31 18:22:40 +00:00
|
|
|
std::string title;
|
|
|
|
|
|
|
|
|
|
// Ensure one argument is given
|
|
|
|
|
|
2017-01-08 16:38:54 +01:00
|
|
|
if(argc != 2) {
|
2005-01-31 18:22:40 +00:00
|
|
|
fprintf(stderr, "Usage: %s <url>\n", argv[0]);
|
2025-02-27 11:32:43 +01:00
|
|
|
return EXIT_FAILURE;
|
2005-01-31 18:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
result = curl_global_init(CURL_GLOBAL_ALL);
|
2026-02-07 16:50:54 +01:00
|
|
|
if(result != CURLE_OK)
|
2025-12-16 15:54:06 +01:00
|
|
|
return (int)result;
|
2005-01-31 20:03:01 +00:00
|
|
|
|
2025-10-31 14:42:30 +01:00
|
|
|
// Initialize CURL handle
|
2005-01-31 18:22:40 +00:00
|
|
|
|
2025-10-31 14:42:30 +01:00
|
|
|
if(!init(curl, argv[1])) {
|
|
|
|
|
fprintf(stderr, "Handle initialization failed\n");
|
2025-10-13 16:30:18 +02:00
|
|
|
curl_global_cleanup();
|
2025-02-27 11:32:43 +01:00
|
|
|
return EXIT_FAILURE;
|
2005-01-31 18:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve content for the URL
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
result = curl_easy_perform(curl);
|
2025-10-31 14:42:30 +01:00
|
|
|
curl_easy_cleanup(curl);
|
2005-01-31 20:03:01 +00:00
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
if(result != CURLE_OK) {
|
2005-01-31 18:22:40 +00:00
|
|
|
fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
|
2025-02-27 11:32:43 +01:00
|
|
|
return EXIT_FAILURE;
|
2005-01-31 18:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse the (assumed) HTML code
|
|
|
|
|
parseHtml(buffer, title);
|
|
|
|
|
|
|
|
|
|
// Display the extracted title
|
|
|
|
|
printf("Title: %s\n", title.c_str());
|
|
|
|
|
|
2025-12-16 15:54:06 +01:00
|
|
|
return (int)result;
|
2005-01-31 18:22:40 +00:00
|
|
|
}
|