113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci/* <DESC>
2613498266Sopenharmony_ci * Uses the "Streaming HTML parser" to extract the href pieces in a streaming
2713498266Sopenharmony_ci * manner from a downloaded HTML.
2813498266Sopenharmony_ci * </DESC>
2913498266Sopenharmony_ci */
3013498266Sopenharmony_ci/*
3113498266Sopenharmony_ci * The HTML parser is found at https://github.com/arjunc77/htmlstreamparser
3213498266Sopenharmony_ci */
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci#include <stdio.h>
3513498266Sopenharmony_ci#include <curl/curl.h>
3613498266Sopenharmony_ci#include <htmlstreamparser.h>
3713498266Sopenharmony_ci
3813498266Sopenharmony_ci
3913498266Sopenharmony_cistatic size_t write_callback(void *buffer, size_t size, size_t nmemb,
4013498266Sopenharmony_ci                             void *hsp)
4113498266Sopenharmony_ci{
4213498266Sopenharmony_ci  size_t realsize = size * nmemb, p;
4313498266Sopenharmony_ci  for(p = 0; p < realsize; p++) {
4413498266Sopenharmony_ci    html_parser_char_parse(hsp, ((char *)buffer)[p]);
4513498266Sopenharmony_ci    if(html_parser_cmp_tag(hsp, "a", 1))
4613498266Sopenharmony_ci      if(html_parser_cmp_attr(hsp, "href", 4))
4713498266Sopenharmony_ci        if(html_parser_is_in(hsp, HTML_VALUE_ENDED)) {
4813498266Sopenharmony_ci          html_parser_val(hsp)[html_parser_val_length(hsp)] = '\0';
4913498266Sopenharmony_ci          printf("%s\n", html_parser_val(hsp));
5013498266Sopenharmony_ci        }
5113498266Sopenharmony_ci  }
5213498266Sopenharmony_ci  return realsize;
5313498266Sopenharmony_ci}
5413498266Sopenharmony_ci
5513498266Sopenharmony_ciint main(int argc, char *argv[])
5613498266Sopenharmony_ci{
5713498266Sopenharmony_ci  char tag[1], attr[4], val[128];
5813498266Sopenharmony_ci  CURL *curl;
5913498266Sopenharmony_ci  HTMLSTREAMPARSER *hsp;
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci  if(argc != 2) {
6213498266Sopenharmony_ci    printf("Usage: %s URL\n", argv[0]);
6313498266Sopenharmony_ci    return EXIT_FAILURE;
6413498266Sopenharmony_ci  }
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  curl = curl_easy_init();
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  hsp = html_parser_init();
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  html_parser_set_tag_to_lower(hsp, 1);
7113498266Sopenharmony_ci  html_parser_set_attr_to_lower(hsp, 1);
7213498266Sopenharmony_ci  html_parser_set_tag_buffer(hsp, tag, sizeof(tag));
7313498266Sopenharmony_ci  html_parser_set_attr_buffer(hsp, attr, sizeof(attr));
7413498266Sopenharmony_ci  html_parser_set_val_buffer(hsp, val, sizeof(val)-1);
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
7713498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
7813498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_WRITEDATA, hsp);
7913498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
8013498266Sopenharmony_ci
8113498266Sopenharmony_ci  curl_easy_perform(curl);
8213498266Sopenharmony_ci
8313498266Sopenharmony_ci  curl_easy_cleanup(curl);
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci  html_parser_cleanup(hsp);
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci  return EXIT_SUCCESS;
8813498266Sopenharmony_ci}
89