toolbox

Check-in [f1da0d93bc]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:http-code: add timeout flag
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:f1da0d93bce862fe47a8e2dce256e7456b342c6882f7d223c99356bf65de2bce
User & Date: jef 2019-05-02 21:32:24
Context
2019-05-02
21:38
http-code: fix mixed indentation typo check-in: cf9ec4c414 user: jef tags: trunk
21:32
http-code: add timeout flag check-in: f1da0d93bc user: jef tags: trunk
2019-04-23
08:43
renew: add checks after popen calls check-in: 454cd3f8a2 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/http-code.md.

10
11
12
13
14
15
16






17
18
19
20
21
22
23
24
*url*

## DESCRIPTION

**http-code**
display the HTTP status code for an url (after HTTP redirects).







## EXAMPLE

	$ http-code https://foutaise.org
	200

## AUTHOR

Gerome Fournier <[jef@foutaise.org](mailto:jef@foutaise.org)>.







>
>
>
>
>
>








10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
*url*

## DESCRIPTION

**http-code**
display the HTTP status code for an url (after HTTP redirects).

## OPTIONS

**-t** *int*

> Specify timeout in milliseconds (10000 by default)

## EXAMPLE

	$ http-code https://foutaise.org
	200

## AUTHOR

Gerome Fournier <[jef@foutaise.org](mailto:jef@foutaise.org)>.

Changes to src/http-code/http-code.1.

6
7
8
9
10
11
12





13
14
15
16
17
18
19
.Nd Display HTTP status code for an url
.Sh SYNOPSIS
.Nm
.Ar url
.Sh DESCRIPTION
.Nm
display the HTTP status code for an url (after HTTP redirects).





.Sh EXAMPLE
.Bd -literal
$ http-code https://foutaise.org
200
.Ed
.Sh AUTHOR
.An Gerome Fournier Aq Mt jef@foutaise.org .







>
>
>
>
>







6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
.Nd Display HTTP status code for an url
.Sh SYNOPSIS
.Nm
.Ar url
.Sh DESCRIPTION
.Nm
display the HTTP status code for an url (after HTTP redirects).
.Sh OPTIONS
.Bl -tag -width indent
.It Fl t Ar int
Specify timeout in milliseconds (10000 by default)
.El
.Sh EXAMPLE
.Bd -literal
$ http-code https://foutaise.org
200
.Ed
.Sh AUTHOR
.An Gerome Fournier Aq Mt jef@foutaise.org .

Changes to src/http-code/http-code.c.

14
15
16
17
18
19
20
21

22
23















24
25
26
27
28
29
30
..
39
40
41
42
43
44
45

46
47
48
49
50
51
52



53

54
55
56
57
58
59
60
..
61
62
63
64
65
66
67

68
69
70
71
72
usage()
{
	fprintf(stderr,
		"Display HTTP status code for an url\n"
		"Usage:\n"
		"	%s url\n"
		"Options:\n"
		"	-h : display this help and exit\n", PROGNAME);

	exit(1);
}
















size_t
write_callback(void *buffer, size_t size, size_t nmemb, void *userdata)
{
	CURL *curl;
	long response_code;

................................................................................
	exit(0);
}

int
main(int argc, char *argv[])
{
	CURL *curl;

	int ch;

	while ((ch = getopt(argc, argv, "h")) != -1) {
		switch (ch) {
		case 'h':
			usage();
			break;



		}

	}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		die("wrong number of arguments");

................................................................................
	curl = curl_easy_init();
	if (curl == NULL)
		die("unable to initialize curl");
	curl_easy_setopt(curl, CURLOPT_URL, argv[0]);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

	curl_easy_setopt(curl, CURLOPT_USERAGENT, "Curl");
	curl_easy_perform(curl);

	return EXIT_SUCCESS;
}







|
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>


|




>
>
>

>







 







>





14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
..
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
..
82
83
84
85
86
87
88
89
90
91
92
93
94
usage()
{
	fprintf(stderr,
		"Display HTTP status code for an url\n"
		"Usage:\n"
		"	%s url\n"
		"Options:\n"
		"	-h           : display this help and exit\n"
		"	-t <timeout> : timeout in milliseconds (10000 by default)\n", PROGNAME);
	exit(1);
}

long
parse_timeout(char *str)
{
	long value;
	int nparsed;

	if (sscanf(str, "%ld%n", &value, &nparsed) == 1
                        && strlen(str) == (size_t) nparsed
			&& value >= 0)
		return value;

	die("invalid timeout argument '%s'", str);
	return 0;
}

size_t
write_callback(void *buffer, size_t size, size_t nmemb, void *userdata)
{
	CURL *curl;
	long response_code;

................................................................................
	exit(0);
}

int
main(int argc, char *argv[])
{
	CURL *curl;
	long timeout = 10000;
	int ch;

	while ((ch = getopt(argc, argv, "ht:")) != -1) {
		switch (ch) {
		case 'h':
			usage();
			break;
		case 't':
			timeout = parse_timeout(optarg);
			break;
		}

	}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		die("wrong number of arguments");

................................................................................
	curl = curl_easy_init();
	if (curl == NULL)
		die("unable to initialize curl");
	curl_easy_setopt(curl, CURLOPT_URL, argv[0]);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout);
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "Curl");
	curl_easy_perform(curl);

	return EXIT_SUCCESS;
}