xref: /curl/docs/examples/href_extractor.c (revision 2bc1d775)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.se/docs/copyright.html.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 /* <DESC>
26  * Uses the "Streaming HTML parser" to extract the href pieces in a streaming
27  * manner from a downloaded HTML.
28  * </DESC>
29  */
30 /*
31  * The HTML parser is found at https://github.com/arjunc77/htmlstreamparser
32  */
33 
34 #include <stdio.h>
35 #include <curl/curl.h>
36 #include <htmlstreamparser.h>
37 
38 
write_callback(void * buffer,size_t size,size_t nmemb,void * hsp)39 static size_t write_callback(void *buffer, size_t size, size_t nmemb,
40                              void *hsp)
41 {
42   size_t realsize = size * nmemb, p;
43   for(p = 0; p < realsize; p++) {
44     html_parser_char_parse(hsp, ((char *)buffer)[p]);
45     if(html_parser_cmp_tag(hsp, "a", 1))
46       if(html_parser_cmp_attr(hsp, "href", 4))
47         if(html_parser_is_in(hsp, HTML_VALUE_ENDED)) {
48           html_parser_val(hsp)[html_parser_val_length(hsp)] = '\0';
49           printf("%s\n", html_parser_val(hsp));
50         }
51   }
52   return realsize;
53 }
54 
main(int argc,char * argv[])55 int main(int argc, char *argv[])
56 {
57   char tag[1], attr[4], val[128];
58   CURL *curl;
59   HTMLSTREAMPARSER *hsp;
60 
61   if(argc != 2) {
62     printf("Usage: %s URL\n", argv[0]);
63     return EXIT_FAILURE;
64   }
65 
66   curl = curl_easy_init();
67 
68   hsp = html_parser_init();
69 
70   html_parser_set_tag_to_lower(hsp, 1);
71   html_parser_set_attr_to_lower(hsp, 1);
72   html_parser_set_tag_buffer(hsp, tag, sizeof(tag));
73   html_parser_set_attr_buffer(hsp, attr, sizeof(attr));
74   html_parser_set_val_buffer(hsp, val, sizeof(val)-1);
75 
76   curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
77   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
78   curl_easy_setopt(curl, CURLOPT_WRITEDATA, hsp);
79   curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
80 
81   curl_easy_perform(curl);
82 
83   curl_easy_cleanup(curl);
84 
85   html_parser_cleanup(hsp);
86 
87   return EXIT_SUCCESS;
88 }
89