2012-01-29 16:34:10 +01:00
|
|
|
/*
|
|
|
|
* Spdylay - SPDY Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <signal.h>
|
2012-01-30 16:46:46 +01:00
|
|
|
#include <getopt.h>
|
2012-01-31 14:04:51 +01:00
|
|
|
#include <poll.h>
|
2012-01-29 16:34:10 +01:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <fstream>
|
2012-01-30 16:46:46 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2012-02-01 13:47:25 +01:00
|
|
|
#include <sstream>
|
2012-01-29 16:34:10 +01:00
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <spdylay/spdylay.h>
|
|
|
|
|
|
|
|
#include "spdylay_ssl.h"
|
|
|
|
#include "uri.h"
|
|
|
|
|
|
|
|
namespace spdylay {
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
struct Config {
|
|
|
|
bool null_out;
|
|
|
|
bool remote_name;
|
|
|
|
bool verbose;
|
2012-02-26 10:13:56 +01:00
|
|
|
bool spdy3_only;
|
2012-04-04 19:19:00 +02:00
|
|
|
int timeout;
|
2012-02-26 10:13:56 +01:00
|
|
|
Config():null_out(false), remote_name(false), verbose(false),
|
2012-04-04 19:19:00 +02:00
|
|
|
spdy3_only(false), timeout(-1) {}
|
2012-01-30 16:46:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Request {
|
|
|
|
uri::UriStruct us;
|
|
|
|
Request(const uri::UriStruct& us):us(us) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<int32_t, Request*> stream2req;
|
|
|
|
size_t numreq, complete;
|
|
|
|
|
|
|
|
Config config;
|
2012-01-29 16:34:10 +01:00
|
|
|
extern bool ssl_debug;
|
|
|
|
|
|
|
|
void on_data_chunk_recv_callback
|
|
|
|
(spdylay_session *session, uint8_t flags, int32_t stream_id,
|
|
|
|
const uint8_t *data, size_t len, void *user_data)
|
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
std::map<int32_t, Request*>::iterator itr = stream2req.find(stream_id);
|
|
|
|
if(itr != stream2req.end()) {
|
2012-01-29 16:34:10 +01:00
|
|
|
std::cout.write(reinterpret_cast<const char*>(data), len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 17:37:21 +01:00
|
|
|
void check_stream_id(spdylay_session *session,
|
|
|
|
spdylay_frame_type type, spdylay_frame *frame)
|
2012-01-29 16:34:10 +01:00
|
|
|
{
|
2012-02-03 17:37:21 +01:00
|
|
|
int32_t stream_id = frame->syn_stream.stream_id;
|
|
|
|
Request *req = (Request*)spdylay_session_get_stream_user_data(session,
|
|
|
|
stream_id);
|
|
|
|
stream2req[stream_id] = req;
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_ctrl_send_callback2
|
|
|
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-02-03 17:37:21 +01:00
|
|
|
if(type == SPDYLAY_SYN_STREAM) {
|
|
|
|
check_stream_id(session, type, frame);
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_ctrl_send_callback3
|
|
|
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-02-03 17:37:21 +01:00
|
|
|
if(type == SPDYLAY_SYN_STREAM) {
|
|
|
|
check_stream_id(session, type, frame);
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
on_ctrl_send_callback(session, type, frame, user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_stream_close_callback
|
|
|
|
(spdylay_session *session, int32_t stream_id, spdylay_status_code status_code,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
std::map<int32_t, Request*>::iterator itr = stream2req.find(stream_id);
|
|
|
|
if(itr != stream2req.end()) {
|
|
|
|
++complete;
|
|
|
|
if(complete == numreq) {
|
2012-02-26 08:26:38 +01:00
|
|
|
spdylay_submit_goaway(session, SPDYLAY_GOAWAY_OK);
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int communicate(const std::string& host, uint16_t port,
|
2012-01-30 16:46:46 +01:00
|
|
|
std::vector<Request>& reqvec,
|
2012-01-29 16:34:10 +01:00
|
|
|
const spdylay_session_callbacks *callbacks)
|
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
numreq = reqvec.size();
|
|
|
|
complete = 0;
|
|
|
|
stream2req.clear();
|
2012-01-29 16:34:10 +01:00
|
|
|
int fd = connect_to(host, port);
|
|
|
|
if(fd == -1) {
|
|
|
|
std::cerr << "Could not connect to the host" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
|
|
|
if(!ssl_ctx) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-25 17:31:45 +01:00
|
|
|
std::string next_proto;
|
2012-02-26 10:13:56 +01:00
|
|
|
if(config.spdy3_only) {
|
|
|
|
next_proto = "spdy/3";
|
|
|
|
}
|
2012-02-25 17:31:45 +01:00
|
|
|
setup_ssl_ctx(ssl_ctx, &next_proto);
|
2012-01-29 16:34:10 +01:00
|
|
|
SSL *ssl = SSL_new(ssl_ctx);
|
|
|
|
if(!ssl) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(ssl_handshake(ssl, fd) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-20 22:15:02 +01:00
|
|
|
make_non_block(fd);
|
|
|
|
set_tcp_nodelay(fd);
|
2012-03-02 19:59:07 +01:00
|
|
|
int spdy_version = spdylay_npn_get_version(
|
|
|
|
reinterpret_cast<const unsigned char*>(next_proto.c_str()),
|
|
|
|
next_proto.size());
|
|
|
|
if (spdy_version <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Spdylay sc(fd, ssl, spdy_version, callbacks);
|
2012-01-31 14:04:51 +01:00
|
|
|
|
|
|
|
nfds_t npollfds = 1;
|
|
|
|
pollfd pollfds[1];
|
|
|
|
|
2012-02-01 13:47:25 +01:00
|
|
|
std::stringstream ss;
|
2012-02-14 16:14:27 +01:00
|
|
|
if(reqvec[0].us.ipv6LiteralAddress) {
|
|
|
|
ss << "[";
|
|
|
|
}
|
|
|
|
ss << host;
|
|
|
|
if(reqvec[0].us.ipv6LiteralAddress) {
|
|
|
|
ss << "]";
|
|
|
|
}
|
2012-03-08 18:36:55 +01:00
|
|
|
if(port != 443) {
|
|
|
|
ss << ":" << port;
|
|
|
|
}
|
2012-02-01 13:47:25 +01:00
|
|
|
std::string hostport = ss.str();
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
for(int i = 0, n = reqvec.size(); i < n; ++i) {
|
|
|
|
uri::UriStruct& us = reqvec[i].us;
|
|
|
|
std::string path = us.dir+us.file+us.query;
|
2012-02-03 17:37:21 +01:00
|
|
|
int r = sc.submit_request(hostport, path, 3, &reqvec[i]);
|
2012-01-30 16:46:46 +01:00
|
|
|
assert(r == 0);
|
|
|
|
}
|
2012-01-31 14:04:51 +01:00
|
|
|
pollfds[0].fd = fd;
|
|
|
|
ctl_poll(pollfds, &sc);
|
2012-04-04 19:19:00 +02:00
|
|
|
int end_time = time(NULL) + config.timeout;
|
|
|
|
int timeout = config.timeout;
|
2012-01-31 14:04:51 +01:00
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
bool ok = true;
|
|
|
|
while(sc.want_read() || sc.want_write()) {
|
2012-04-04 19:19:00 +02:00
|
|
|
int nfds = poll(pollfds, npollfds, timeout);
|
2012-01-29 16:34:10 +01:00
|
|
|
if(nfds == -1) {
|
2012-01-31 14:04:51 +01:00
|
|
|
perror("poll");
|
2012-01-29 16:34:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-07 17:33:55 +01:00
|
|
|
if(pollfds[0].revents & (POLLIN | POLLOUT)) {
|
|
|
|
if(sc.recv() != 0 || sc.send() != 0) {
|
2012-01-29 16:34:10 +01:00
|
|
|
std::cout << "Fatal" << std::endl;
|
2012-04-04 19:19:00 +02:00
|
|
|
ok = false;
|
2012-01-29 16:34:10 +01:00
|
|
|
break;
|
2012-02-07 17:33:55 +01:00
|
|
|
}
|
2012-01-31 14:04:51 +01:00
|
|
|
}
|
|
|
|
if((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) {
|
|
|
|
std::cout << "HUP" << std::endl;
|
|
|
|
ok = false;
|
|
|
|
break;
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
2012-04-04 19:19:00 +02:00
|
|
|
timeout = timeout == -1 ? timeout : end_time - time(NULL);
|
|
|
|
if (config.timeout != -1 && timeout <= 0) {
|
|
|
|
std::cout << "Requests to " << hostport << "timed out.";
|
|
|
|
ok = false;
|
2012-01-29 16:34:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-04-04 19:19:00 +02:00
|
|
|
assert(ok);
|
2012-01-31 14:04:51 +01:00
|
|
|
ctl_poll(pollfds, &sc);
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SSL_shutdown(ssl);
|
|
|
|
SSL_free(ssl);
|
|
|
|
SSL_CTX_free(ssl_ctx);
|
|
|
|
shutdown(fd, SHUT_WR);
|
|
|
|
close(fd);
|
|
|
|
return ok ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
int run(char **uris, int n)
|
2012-01-29 16:34:10 +01:00
|
|
|
{
|
|
|
|
spdylay_session_callbacks callbacks;
|
|
|
|
memset(&callbacks, 0, sizeof(spdylay_session_callbacks));
|
|
|
|
callbacks.send_callback = send_callback;
|
|
|
|
callbacks.recv_callback = recv_callback;
|
|
|
|
callbacks.on_stream_close_callback = on_stream_close_callback;
|
2012-01-30 16:46:46 +01:00
|
|
|
if(config.verbose) {
|
2012-01-29 16:34:10 +01:00
|
|
|
callbacks.on_ctrl_recv_callback = on_ctrl_recv_callback;
|
|
|
|
callbacks.on_data_recv_callback = on_data_recv_callback;
|
|
|
|
callbacks.on_ctrl_send_callback = on_ctrl_send_callback3;
|
|
|
|
} else {
|
|
|
|
callbacks.on_ctrl_send_callback = on_ctrl_send_callback2;
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
|
|
|
if(!config.null_out) {
|
2012-01-29 16:34:10 +01:00
|
|
|
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
|
|
|
|
}
|
2012-01-30 16:46:46 +01:00
|
|
|
ssl_debug = config.verbose;
|
|
|
|
std::vector<Request> reqvec;
|
|
|
|
std::string prev_host;
|
|
|
|
uint16_t prev_port = 0;
|
2012-03-02 19:59:07 +01:00
|
|
|
int failures = 0;
|
2012-01-30 16:46:46 +01:00
|
|
|
for(int i = 0; i < n; ++i) {
|
|
|
|
uri::UriStruct us;
|
|
|
|
if(uri::parse(us, uris[i])) {
|
|
|
|
if(prev_host != us.host || prev_port != us.port) {
|
|
|
|
if(!reqvec.empty()) {
|
2012-03-02 19:59:07 +01:00
|
|
|
if (communicate(prev_host, prev_port, reqvec, &callbacks) != 0) {
|
|
|
|
++failures;
|
|
|
|
}
|
2012-01-30 16:46:46 +01:00
|
|
|
reqvec.clear();
|
|
|
|
}
|
|
|
|
prev_host = us.host;
|
|
|
|
prev_port = us.port;
|
|
|
|
}
|
|
|
|
reqvec.push_back(Request(us));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!reqvec.empty()) {
|
2012-03-02 19:59:07 +01:00
|
|
|
if (communicate(prev_host, prev_port, reqvec, &callbacks) != 0) {
|
|
|
|
++failures;
|
|
|
|
}
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
2012-03-02 19:59:07 +01:00
|
|
|
return failures;
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
void print_usage(std::ostream& out)
|
|
|
|
{
|
2012-04-04 19:19:00 +02:00
|
|
|
out << "Usage: spdycat [-Onv3] [--timeout=seconds] [URI...]" << std::endl;
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
void print_help(std::ostream& out)
|
2012-01-29 16:34:10 +01:00
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
print_usage(out);
|
|
|
|
out << "\n"
|
|
|
|
<< "OPTIONS:\n"
|
|
|
|
<< " -v, --verbose Print debug information such as reception/\n"
|
|
|
|
<< " transmission of frames and name/value pairs.\n"
|
|
|
|
<< " -n, --null-out Discard downloaded data.\n"
|
|
|
|
<< " -O, --remote-name Save download data in the current directory.\n"
|
|
|
|
<< " The filename is dereived from URI. If URI\n"
|
|
|
|
<< " ends with '/', 'index.html' is used as a\n"
|
|
|
|
<< " filename. Not implemented yet.\n"
|
2012-02-26 10:13:56 +01:00
|
|
|
<< " -3, --spdy3 Only use SPDY/3.\n"
|
2012-04-04 19:19:00 +02:00
|
|
|
<< " -t, --timeout=N Timeout each request after N seconds.\n"
|
2012-02-26 10:13:56 +01:00
|
|
|
<< "\n"
|
2012-01-30 16:46:46 +01:00
|
|
|
<< std::endl;
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
while(1) {
|
|
|
|
static option long_options[] = {
|
|
|
|
{"verbose", no_argument, 0, 'v' },
|
|
|
|
{"null-out", no_argument, 0, 'n' },
|
|
|
|
{"remote-name", no_argument, 0, 'O' },
|
2012-02-26 10:13:56 +01:00
|
|
|
{"spdy3", no_argument, 0, '3' },
|
2012-04-04 19:19:00 +02:00
|
|
|
{"timeout", required_argument, 0, 't' },
|
2012-01-30 16:46:46 +01:00
|
|
|
{"help", no_argument, 0, 'h' },
|
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
int option_index = 0;
|
2012-04-04 19:19:00 +02:00
|
|
|
int c = getopt_long(argc, argv, "Onhv3t", long_options, &option_index);
|
2012-01-30 16:46:46 +01:00
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(c) {
|
|
|
|
case 'O':
|
|
|
|
config.remote_name = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
print_help(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'n':
|
|
|
|
config.null_out = true;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
config.verbose = true;
|
|
|
|
break;
|
2012-02-26 10:13:56 +01:00
|
|
|
case '3':
|
|
|
|
config.spdy3_only = true;
|
|
|
|
break;
|
2012-04-04 19:19:00 +02:00
|
|
|
case 't':
|
|
|
|
config.timeout = atoi(optarg);
|
|
|
|
break;
|
2012-01-30 16:46:46 +01:00
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &act, 0);
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
2012-01-30 16:46:46 +01:00
|
|
|
reset_timer();
|
2012-03-02 19:59:07 +01:00
|
|
|
return run(argv+optind, argc-optind);
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
2012-01-30 16:46:46 +01:00
|
|
|
|
|
|
|
} // namespace spdylay
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return spdylay::main(argc, argv);
|
|
|
|
}
|