Skip to content

Commit 29ef787

Browse files
committed
lab5 materials
1 parent ca8a2a5 commit 29ef787

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

‎ece252_manual.pdf‎

12.5 KB
Binary file not shown.

‎lab5/starter/curl_multi/Makefile‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Makefile, ECE252
2+
# Yiqing Huang
3+
4+
CC = gcc
5+
CFLAGS_LIBS = $(shell pkg-config --cflags libxml-2.0 libcurl)
6+
CFLAGS = -Wall $(CFLAGS_LIBS) -std=gnu99 -g
7+
LD = gcc
8+
LDFLAGS = -std=gnu99 -g
9+
LDLIBS = $(shell pkg-config --libs libxml-2.0 libcurl)
10+
11+
SRCS = curl_multi_test.c
12+
OBJS1 = curl_multi_test.o
13+
TARGETS= curl_multi_test.out
14+
15+
all: ${TARGETS}
16+
17+
curl_multi_test.out: $(OBJS1)
18+
$(LD) -o $@ $^ $(LDLIBS) $(LDFLAGS)
19+
20+
%.o: %.c
21+
$(CC) $(CFLAGS) -c $<
22+
23+
%.d: %.c
24+
gcc -MM -MF $@ $<
25+
26+
-include $(SRCS:.c=.d)
27+
28+
.PHONY: clean
29+
clean:
30+
rm -f *~ *.d *.o $(TARGETS)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* curl_multi_test.c
2+
3+
Clemens Gruber, 2013
4+
<clemens.gruber@pqgruber.com>
5+
6+
Code description:
7+
Requests 4 Web pages via the CURL multi interface
8+
and checks if the HTTP status code is 200.
9+
10+
Update: Fixed! The check for !numfds was the problem.
11+
*/
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#ifndef WIN32
16+
#include <unistd.h>
17+
#endif
18+
#include <curl/multi.h>
19+
20+
#define MAX_WAIT_MSECS 30*1000 /* Wait max. 30 seconds */
21+
22+
static const char *urls[] = {
23+
"http://www.microsoft.com",
24+
"http://www.yahoo.com",
25+
"http://www.wikipedia.org",
26+
"http://slashdot.org"
27+
};
28+
#define CNT 4
29+
30+
static size_t cb(char *d, size_t n, size_t l, void *p)
31+
{
32+
/* take care of the data here, ignored in this example */
33+
(void)d;
34+
(void)p;
35+
return n*l;
36+
}
37+
38+
static void init(CURLM *cm, int i)
39+
{
40+
CURL *eh = curl_easy_init();
41+
curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
42+
curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
43+
curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
44+
curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
45+
curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
46+
curl_multi_add_handle(cm, eh);
47+
}
48+
49+
int main(void)
50+
{
51+
CURLM *cm=NULL;
52+
CURL *eh=NULL;
53+
CURLMsg *msg=NULL;
54+
CURLcode return_code=0;
55+
int still_running=0, i=0, msgs_left=0;
56+
int http_status_code;
57+
const char *szUrl;
58+
59+
curl_global_init(CURL_GLOBAL_ALL);
60+
61+
cm = curl_multi_init();
62+
63+
for (i = 0; i < CNT; ++i) {
64+
init(cm, i);
65+
}
66+
67+
curl_multi_perform(cm, &still_running);
68+
69+
do {
70+
int numfds=0;
71+
int res = curl_multi_wait(cm, NULL, 0, MAX_WAIT_MSECS, &numfds);
72+
if(res != CURLM_OK) {
73+
fprintf(stderr, "error: curl_multi_wait() returned %d\n", res);
74+
return EXIT_FAILURE;
75+
}
76+
/*
77+
if(!numfds) {
78+
fprintf(stderr, "error: curl_multi_wait() numfds=%d\n", numfds);
79+
return EXIT_FAILURE;
80+
}
81+
*/
82+
curl_multi_perform(cm, &still_running);
83+
84+
} while(still_running);
85+
86+
while ((msg = curl_multi_info_read(cm, &msgs_left))) {
87+
if (msg->msg == CURLMSG_DONE) {
88+
eh = msg->easy_handle;
89+
90+
return_code = msg->data.result;
91+
if(return_code!=CURLE_OK) {
92+
fprintf(stderr, "CURL error code: %d\n", msg->data.result);
93+
continue;
94+
}
95+
96+
// Get HTTP status code
97+
http_status_code=0;
98+
szUrl = NULL;
99+
100+
curl_easy_getinfo(eh, CURLINFO_RESPONSE_CODE, &http_status_code);
101+
curl_easy_getinfo(eh, CURLINFO_PRIVATE, &szUrl);
102+
103+
if(http_status_code==200) {
104+
printf("200 OK for %s\n", szUrl);
105+
} else {
106+
fprintf(stderr, "GET of %s returned http status code %d\n", szUrl, http_status_code);
107+
}
108+
109+
curl_multi_remove_handle(cm, eh);
110+
curl_easy_cleanup(eh);
111+
}
112+
else {
113+
fprintf(stderr, "error: after curl_multi_info_read(), CURLMsg=%d\n", msg->msg);
114+
}
115+
}
116+
117+
curl_multi_cleanup(cm);
118+
119+
return EXIT_SUCCESS;
120+
}

0 commit comments

Comments
 (0)