1
1
#include <stdlib.h>
2
+ #include <stdio.h>
2
3
#include <unistd.h>
3
4
#include <errno.h>
4
5
#include <sys/select.h>
6
+
7
+ #include <sys/types.h>
8
+ #include <sys/socket.h>
9
+ #include <netdb.h>
10
+ #include <string.h>
11
+
5
12
#include "../include/hyper.h"
6
13
7
14
@@ -64,31 +71,76 @@ static size_t write_cb(void *userdata, hyper_waker *waker, const uint8_t *buf, s
64
71
}
65
72
}
66
73
67
- int main () {
74
+ static int connect_to (char * host , char * port ) {
75
+ struct addrinfo hints ;
76
+ memset (& hints , 0 , sizeof (struct addrinfo ));
77
+ hints .ai_family = AF_UNSPEC ;
78
+ hints .ai_socktype = SOCK_STREAM ;
79
+
80
+ struct addrinfo * result , * rp ;
81
+ if (getaddrinfo (host , port , & hints , & result ) != 0 ) {
82
+ return -1 ;
83
+ }
84
+
85
+ int sfd ;
86
+ for (rp = result ; rp != NULL ; rp = rp -> ai_next ) {
87
+ sfd = socket (rp -> ai_family , rp -> ai_socktype , rp -> ai_protocol );
88
+ if (sfd == -1 ) {
89
+ continue ;
90
+ }
91
+
92
+ if (connect (sfd , rp -> ai_addr , rp -> ai_addrlen ) != -1 ) {
93
+ break ;
94
+ } else {
95
+ close (sfd );
96
+ }
97
+ }
98
+
99
+ freeaddrinfo (result );
100
+
101
+ // no address succeeded
102
+ if (rp == NULL ) {
103
+ return -1 ;
104
+ }
105
+
106
+ return sfd ;
107
+ }
108
+
109
+ static hyper_iter_step print_each_header (void * userdata , hyper_str name , hyper_str value ) {
110
+ printf ("%.*s: %.*s" , (int ) name .len , name .buf , (int ) value .len , value .buf );
111
+ return HYPER_IT_CONTINUE ;
112
+ }
113
+
114
+ int main (int argc , char * argv []) {
115
+
116
+ int fd = connect_to ("httpbin.org" , "80" );
117
+ if (fd < 0 ) {
118
+ return 1 ;
119
+ }
120
+
68
121
69
122
struct conn_fds * all_fds = malloc (sizeof (struct conn_fds ));
70
123
124
+ FD_ZERO (& all_fds -> read );
125
+ FD_ZERO (& all_fds -> write );
126
+ FD_ZERO (& all_fds -> excep );
127
+
71
128
struct conn_data * conn = malloc (sizeof (struct conn_data ));
72
- //TODO: YIKES
73
- // should do a connect() instead
74
- conn -> fd = 3 ;
129
+
130
+ conn -> fd = fd ;
75
131
conn -> all_fds = all_fds ;
76
132
conn -> read_waker = NULL ;
77
133
conn -> write_waker = NULL ;
78
134
79
135
80
- FD_ZERO (& all_fds -> read );
81
- FD_ZERO (& all_fds -> write );
82
- FD_ZERO (& all_fds -> excep );
83
-
84
136
// Hookup the IO
85
137
hyper_io * io = hyper_io_new ();
86
138
hyper_io_set_data (io , (void * )conn );
87
139
hyper_io_set_read (io , read_cb );
88
140
hyper_io_set_write (io , write_cb );
89
141
90
142
// Prepare client options
91
- hyper_clientconn_options * opts = hyper_clientconn_options_new ();
143
+ hyper_clientconn_options * opts = NULL ; // hyper_clientconn_options_new();
92
144
93
145
hyper_task * handshake = hyper_clientconn_handshake (io , opts );
94
146
@@ -124,8 +176,8 @@ int main() {
124
176
125
177
hyper_executor_push (exec , task );
126
178
127
- // TODO: NEXT
128
- /*
179
+ hyper_response * resp ;
180
+
129
181
while (1 ) {
130
182
hyper_executor_poll (exec );
131
183
while (1 ) {
@@ -134,18 +186,31 @@ int main() {
134
186
break ;
135
187
}
136
188
switch (hyper_task_type (task )) {
137
- case HYPER_TASK_CLIENTCONN_HANDSHAKE :
189
+ case HYPER_TASK_CLIENT_SEND :
138
190
// Take the results
139
- client = hyper_task_value(task);
140
- break;
191
+ resp = hyper_task_value (task );
192
+ // fall through
141
193
default :
142
194
hyper_task_free (task );
143
195
break ;
144
196
}
145
197
}
146
198
199
+ // If the response is ready, break out for now...
200
+ if (resp != NULL ) {
201
+ break ;
202
+ }
203
+
147
204
select (1 , & all_fds -> read , & all_fds -> write , & all_fds -> excep , NULL );
148
205
149
206
}
150
- */
207
+
208
+ uint16_t http_status = hyper_response_status (resp );
209
+
210
+ printf ("HTTP Status: %d" , http_status );
211
+
212
+ hyper_headers * headers = hyper_response_headers (resp );
213
+ hyper_headers_iter (headers , print_each_header , NULL );
214
+
215
+ return 0 ;
151
216
}
0 commit comments