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.
|
|
|
|
*/
|
2012-07-27 15:11:13 +02:00
|
|
|
#include "spdylay_config.h"
|
2012-05-19 10:05:12 +02:00
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
#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"
|
2012-05-19 10:05:12 +02:00
|
|
|
#include "HtmlParser.h"
|
|
|
|
#include "util.h"
|
2012-01-29 16:34:10 +01:00
|
|
|
|
|
|
|
namespace spdylay {
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
struct Config {
|
|
|
|
bool null_out;
|
|
|
|
bool remote_name;
|
|
|
|
bool verbose;
|
2012-05-19 10:05:12 +02:00
|
|
|
bool get_assets;
|
2012-05-19 14:49:00 +02:00
|
|
|
bool stat;
|
2012-05-12 14:20:19 +02:00
|
|
|
int spdy_version;
|
2012-04-04 19:19:00 +02:00
|
|
|
int timeout;
|
2012-04-22 16:04:55 +02:00
|
|
|
std::string certfile;
|
|
|
|
std::string keyfile;
|
2012-05-08 14:46:44 +02:00
|
|
|
int window_bits;
|
2012-08-20 21:03:55 +02:00
|
|
|
std::map<std::string,std::string> headers;
|
2012-02-26 10:13:56 +01:00
|
|
|
Config():null_out(false), remote_name(false), verbose(false),
|
2012-05-19 14:49:00 +02:00
|
|
|
get_assets(false), stat(false),
|
|
|
|
spdy_version(-1), timeout(-1), window_bits(-1)
|
2012-05-19 10:05:12 +02:00
|
|
|
{}
|
2012-01-30 16:46:46 +01:00
|
|
|
};
|
|
|
|
|
2012-05-19 14:49:00 +02:00
|
|
|
struct RequestStat {
|
|
|
|
timeval on_syn_stream_time;
|
|
|
|
timeval on_syn_reply_time;
|
|
|
|
timeval on_complete_time;
|
|
|
|
RequestStat()
|
|
|
|
{
|
|
|
|
on_syn_stream_time.tv_sec = -1;
|
|
|
|
on_syn_stream_time.tv_usec = -1;
|
|
|
|
on_syn_reply_time.tv_sec = -1;
|
|
|
|
on_syn_reply_time.tv_usec = -1;
|
|
|
|
on_complete_time.tv_sec = -1;
|
|
|
|
on_complete_time.tv_usec = -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void record_time(timeval *tv)
|
|
|
|
{
|
|
|
|
gettimeofday(tv, 0);
|
|
|
|
}
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
struct Request {
|
|
|
|
uri::UriStruct us;
|
2012-05-07 15:56:28 +02:00
|
|
|
spdylay_gzip *inflater;
|
2012-05-19 10:05:12 +02:00
|
|
|
HtmlParser *html_parser;
|
|
|
|
// Recursion level: 0: first entity, 1: entity linked from first entity
|
|
|
|
int level;
|
2012-05-19 14:49:00 +02:00
|
|
|
RequestStat stat;
|
|
|
|
std::string status;
|
2012-05-19 10:05:12 +02:00
|
|
|
Request(const uri::UriStruct& us, int level = 0)
|
|
|
|
: us(us), inflater(0), html_parser(0), level(level)
|
|
|
|
{}
|
|
|
|
|
2012-04-24 16:48:05 +02:00
|
|
|
~Request()
|
|
|
|
{
|
2012-05-07 15:56:28 +02:00
|
|
|
spdylay_gzip_inflate_del(inflater);
|
2012-05-19 10:05:12 +02:00
|
|
|
delete html_parser;
|
2012-04-24 16:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_inflater()
|
|
|
|
{
|
2012-05-07 15:56:28 +02:00
|
|
|
int rv;
|
|
|
|
rv = spdylay_gzip_inflate_new(&inflater);
|
|
|
|
assert(rv == 0);
|
2012-04-24 16:48:05 +02:00
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
|
|
|
|
void init_html_parser()
|
|
|
|
{
|
|
|
|
html_parser = new HtmlParser(uri::construct(us));
|
|
|
|
}
|
|
|
|
|
|
|
|
int update_html_parser(const uint8_t *data, size_t len, int fin)
|
|
|
|
{
|
|
|
|
if(!html_parser) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int rv;
|
|
|
|
rv = html_parser->parse_chunk(reinterpret_cast<const char*>(data), len,
|
|
|
|
fin);
|
|
|
|
return rv;
|
|
|
|
}
|
2012-05-19 14:49:00 +02:00
|
|
|
|
|
|
|
void record_syn_stream_time()
|
|
|
|
{
|
|
|
|
record_time(&stat.on_syn_stream_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void record_syn_reply_time()
|
|
|
|
{
|
|
|
|
record_time(&stat.on_syn_reply_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void record_complete_time()
|
|
|
|
{
|
|
|
|
record_time(&stat.on_complete_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SessionStat {
|
|
|
|
timeval on_handshake_time;
|
|
|
|
SessionStat()
|
|
|
|
{
|
|
|
|
on_handshake_time.tv_sec = -1;
|
|
|
|
on_handshake_time.tv_usec = -1;
|
|
|
|
}
|
2012-01-30 16:46:46 +01:00
|
|
|
};
|
|
|
|
|
2012-05-19 10:05:12 +02:00
|
|
|
struct SpdySession {
|
|
|
|
std::vector<Request*> reqvec;
|
|
|
|
// Map from stream ID to Request object.
|
|
|
|
std::map<int32_t, Request*> streams;
|
|
|
|
// Insert path already added in reqvec to prevent multiple request
|
|
|
|
// for 1 resource.
|
|
|
|
std::set<std::string> path_cache;
|
|
|
|
// The number of completed requests, including failed ones.
|
|
|
|
size_t complete;
|
|
|
|
std::string hostport;
|
|
|
|
Spdylay *sc;
|
2012-05-19 14:49:00 +02:00
|
|
|
SessionStat stat;
|
2012-05-19 10:05:12 +02:00
|
|
|
SpdySession():complete(0) {}
|
|
|
|
~SpdySession()
|
|
|
|
{
|
|
|
|
for(size_t i = 0; i < reqvec.size(); ++i) {
|
|
|
|
delete reqvec[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool all_requests_processed() const
|
|
|
|
{
|
|
|
|
return complete == reqvec.size();
|
|
|
|
}
|
|
|
|
void update_hostport()
|
|
|
|
{
|
|
|
|
if(reqvec.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::stringstream ss;
|
|
|
|
if(reqvec[0]->us.ipv6LiteralAddress) {
|
|
|
|
ss << "[" << reqvec[0]->us.host << "]";
|
|
|
|
} else {
|
|
|
|
ss << reqvec[0]->us.host;
|
|
|
|
}
|
|
|
|
if(reqvec[0]->us.port != 443) {
|
|
|
|
ss << ":" << reqvec[0]->us.port;
|
|
|
|
}
|
|
|
|
hostport = ss.str();
|
|
|
|
}
|
|
|
|
bool add_request(const uri::UriStruct& us, int level = 0)
|
|
|
|
{
|
|
|
|
std::string key = us.dir+us.file+us.query;
|
|
|
|
if(path_cache.count(key)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
path_cache.insert(key);
|
|
|
|
reqvec.push_back(new Request(us, level));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-05-19 14:49:00 +02:00
|
|
|
void record_handshake_time()
|
|
|
|
{
|
|
|
|
record_time(&stat.on_handshake_time);
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
};
|
2012-01-30 16:46:46 +01:00
|
|
|
|
|
|
|
Config config;
|
2012-01-29 16:34:10 +01:00
|
|
|
extern bool ssl_debug;
|
|
|
|
|
2012-08-21 15:02:35 +02:00
|
|
|
void submit_request(Spdylay& sc, const std::string& hostport,
|
|
|
|
const std::map<std::string,std::string> &headers,
|
|
|
|
Request* req)
|
2012-05-19 10:05:12 +02:00
|
|
|
{
|
|
|
|
uri::UriStruct& us = req->us;
|
|
|
|
std::string path = us.dir+us.file+us.query;
|
2012-08-20 21:03:55 +02:00
|
|
|
int r = sc.submit_request(hostport, path, headers, 3, req);
|
2012-05-19 10:05:12 +02:00
|
|
|
assert(r == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_html_parser(SpdySession *spdySession, Request *req,
|
|
|
|
const uint8_t *data, size_t len, int fin)
|
|
|
|
{
|
|
|
|
if(!req->html_parser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
req->update_html_parser(data, len, fin);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < req->html_parser->get_links().size(); ++i) {
|
|
|
|
const std::string& uri = req->html_parser->get_links()[i];
|
|
|
|
uri::UriStruct us;
|
|
|
|
if(uri::parse(us, uri) &&
|
|
|
|
req->us.protocol == us.protocol && req->us.host == us.host &&
|
|
|
|
req->us.port == us.port) {
|
|
|
|
spdySession->add_request(us, req->level+1);
|
2012-08-20 21:03:55 +02:00
|
|
|
submit_request(*spdySession->sc, spdySession->hostport, config.headers,
|
2012-05-19 10:05:12 +02:00
|
|
|
spdySession->reqvec.back());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req->html_parser->clear_links();
|
|
|
|
}
|
|
|
|
|
|
|
|
SpdySession* get_session(void *user_data)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<SpdySession*>
|
|
|
|
(reinterpret_cast<Spdylay*>(user_data)->user_data());
|
|
|
|
}
|
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
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-05-19 10:05:12 +02:00
|
|
|
SpdySession *spdySession = get_session(user_data);
|
|
|
|
std::map<int32_t, Request*>::iterator itr =
|
|
|
|
spdySession->streams.find(stream_id);
|
|
|
|
if(itr != spdySession->streams.end()) {
|
2012-04-24 16:48:05 +02:00
|
|
|
Request *req = (*itr).second;
|
|
|
|
if(req->inflater) {
|
|
|
|
while(len > 0) {
|
|
|
|
const size_t MAX_OUTLEN = 4096;
|
|
|
|
uint8_t out[MAX_OUTLEN];
|
|
|
|
size_t outlen = MAX_OUTLEN;
|
|
|
|
size_t tlen = len;
|
2012-05-07 15:56:28 +02:00
|
|
|
int rv = spdylay_gzip_inflate(req->inflater, out, &outlen, data, &tlen);
|
2012-04-24 16:48:05 +02:00
|
|
|
if(rv == -1) {
|
|
|
|
spdylay_submit_rst_stream(session, stream_id, SPDYLAY_INTERNAL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
if(!config.null_out) {
|
|
|
|
std::cout.write(reinterpret_cast<const char*>(out), outlen);
|
|
|
|
}
|
|
|
|
update_html_parser(spdySession, req, out, outlen, 0);
|
2012-04-24 16:48:05 +02:00
|
|
|
data += tlen;
|
|
|
|
len -= tlen;
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-19 10:05:12 +02:00
|
|
|
if(!config.null_out) {
|
|
|
|
std::cout.write(reinterpret_cast<const char*>(data), len);
|
|
|
|
}
|
|
|
|
update_html_parser(spdySession, req, data, len, 0);
|
2012-04-24 16:48:05 +02:00
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 17:37:21 +01:00
|
|
|
void check_stream_id(spdylay_session *session,
|
2012-05-19 10:05:12 +02:00
|
|
|
spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data)
|
2012-01-29 16:34:10 +01:00
|
|
|
{
|
2012-05-19 10:05:12 +02:00
|
|
|
SpdySession *spdySession = get_session(user_data);
|
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);
|
2012-05-19 10:05:12 +02:00
|
|
|
spdySession->streams[stream_id] = req;
|
2012-05-19 14:49:00 +02:00
|
|
|
req->record_syn_stream_time();
|
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) {
|
2012-05-19 10:05:12 +02:00
|
|
|
check_stream_id(session, type, frame, user_data);
|
2012-02-03 17:37:21 +01:00
|
|
|
}
|
2012-05-19 14:49:00 +02:00
|
|
|
if(config.verbose) {
|
|
|
|
on_ctrl_send_callback(session, type, frame, user_data);
|
2012-02-03 17:37:21 +01:00
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
|
2012-05-19 10:05:12 +02:00
|
|
|
void check_response_header
|
2012-04-24 16:48:05 +02:00
|
|
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
char **nv;
|
|
|
|
int32_t stream_id;
|
|
|
|
if(type == SPDYLAY_SYN_REPLY) {
|
|
|
|
nv = frame->syn_reply.nv;
|
|
|
|
stream_id = frame->syn_reply.stream_id;
|
|
|
|
} else if(type == SPDYLAY_HEADERS) {
|
|
|
|
nv = frame->headers.nv;
|
|
|
|
stream_id = frame->headers.stream_id;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
Request *req = (Request*)spdylay_session_get_stream_user_data(session,
|
|
|
|
stream_id);
|
|
|
|
assert(req);
|
2012-04-24 16:48:05 +02:00
|
|
|
bool gzip = false;
|
|
|
|
for(size_t i = 0; nv[i]; i += 2) {
|
|
|
|
if(strcmp("content-encoding", nv[i]) == 0) {
|
2012-05-19 10:05:12 +02:00
|
|
|
gzip = util::strieq("gzip", nv[i+1]) || util::strieq("deflate", nv[i+1]);
|
2012-05-19 14:49:00 +02:00
|
|
|
} else if(strcmp(":status", nv[i]) == 0) {
|
|
|
|
req->status = nv[i+1];
|
2012-04-24 16:48:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(gzip) {
|
2012-05-19 10:05:12 +02:00
|
|
|
if(!req->inflater) {
|
|
|
|
req->init_inflater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(config.get_assets && req->level == 0) {
|
|
|
|
if(!req->html_parser) {
|
|
|
|
req->init_html_parser();
|
2012-04-24 16:48:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_ctrl_recv_callback2
|
|
|
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-05-19 14:49:00 +02:00
|
|
|
if(type == SPDYLAY_SYN_REPLY) {
|
|
|
|
Request *req = (Request*)spdylay_session_get_stream_user_data
|
|
|
|
(session, frame->syn_reply.stream_id);
|
|
|
|
assert(req);
|
|
|
|
req->record_syn_reply_time();
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
check_response_header(session, type, frame, user_data);
|
2012-05-19 14:49:00 +02:00
|
|
|
if(config.verbose) {
|
|
|
|
on_ctrl_recv_callback(session, type, frame, user_data);
|
|
|
|
}
|
2012-04-24 16:48:05 +02:00
|
|
|
}
|
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
void on_stream_close_callback
|
|
|
|
(spdylay_session *session, int32_t stream_id, spdylay_status_code status_code,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-05-19 10:05:12 +02:00
|
|
|
SpdySession *spdySession = get_session(user_data);
|
|
|
|
std::map<int32_t, Request*>::iterator itr =
|
|
|
|
spdySession->streams.find(stream_id);
|
|
|
|
if(itr != spdySession->streams.end()) {
|
|
|
|
update_html_parser(spdySession, (*itr).second, 0, 0, 1);
|
2012-05-19 14:49:00 +02:00
|
|
|
(*itr).second->record_complete_time();
|
2012-05-19 10:05:12 +02:00
|
|
|
++spdySession->complete;
|
|
|
|
if(spdySession->all_requests_processed()) {
|
2012-06-14 13:07:00 +02:00
|
|
|
spdylay_submit_goaway(session, SPDYLAY_GOAWAY_OK);
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-19 14:49:00 +02:00
|
|
|
int64_t time_delta(const timeval& a, const timeval& b)
|
|
|
|
{
|
|
|
|
int64_t res;
|
|
|
|
res = a.tv_usec - b.tv_usec;
|
|
|
|
int off = 0;
|
|
|
|
if(res < 0) {
|
|
|
|
res += 1000000;
|
|
|
|
off = -1;
|
|
|
|
}
|
|
|
|
res += (a.tv_sec + off - b.tv_sec)*1000000;
|
|
|
|
return res/1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_stats(const SpdySession& spdySession)
|
|
|
|
{
|
|
|
|
std::cout << "***** Statistics *****" << std::endl;
|
|
|
|
for(size_t i = 0; i < spdySession.reqvec.size(); ++i) {
|
|
|
|
const Request *req = spdySession.reqvec[i];
|
|
|
|
std::cout << "#" << i+1 << ": " << uri::construct(req->us) << std::endl;
|
|
|
|
std::cout << " Status: " << req->status << std::endl;
|
|
|
|
std::cout << " Delta (ms) from SSL/TLS handshake(SYN_STREAM):"
|
|
|
|
<< std::endl;
|
|
|
|
if(req->stat.on_syn_reply_time.tv_sec >= 0) {
|
|
|
|
std::cout << " SYN_REPLY: "
|
|
|
|
<< time_delta(req->stat.on_syn_reply_time,
|
|
|
|
spdySession.stat.on_handshake_time)
|
|
|
|
<< "("
|
|
|
|
<< time_delta(req->stat.on_syn_reply_time,
|
|
|
|
req->stat.on_syn_stream_time)
|
|
|
|
<< ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
if(req->stat.on_complete_time.tv_sec >= 0) {
|
|
|
|
std::cout << " Completed: "
|
|
|
|
<< time_delta(req->stat.on_complete_time,
|
|
|
|
spdySession.stat.on_handshake_time)
|
|
|
|
<< "("
|
|
|
|
<< time_delta(req->stat.on_complete_time,
|
|
|
|
req->stat.on_syn_stream_time)
|
|
|
|
<< ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
int communicate(const std::string& host, uint16_t port,
|
2012-05-19 10:05:12 +02:00
|
|
|
SpdySession& spdySession,
|
2012-01-29 16:34:10 +01:00
|
|
|
const spdylay_session_callbacks *callbacks)
|
|
|
|
{
|
|
|
|
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;
|
2012-07-13 16:05:24 +02:00
|
|
|
ssl_ctx = SSL_CTX_new(TLSv1_client_method());
|
2012-01-29 16:34:10 +01:00
|
|
|
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-05-12 14:20:19 +02:00
|
|
|
switch(config.spdy_version) {
|
|
|
|
case SPDYLAY_PROTO_SPDY2:
|
|
|
|
next_proto = "spdy/2";
|
|
|
|
break;
|
|
|
|
case SPDYLAY_PROTO_SPDY3:
|
2012-02-26 10:13:56 +01:00
|
|
|
next_proto = "spdy/3";
|
2012-05-12 14:20:19 +02:00
|
|
|
break;
|
2012-02-26 10:13:56 +01:00
|
|
|
}
|
2012-02-25 17:31:45 +01:00
|
|
|
setup_ssl_ctx(ssl_ctx, &next_proto);
|
2012-04-22 16:04:55 +02:00
|
|
|
if(!config.keyfile.empty()) {
|
|
|
|
if(SSL_CTX_use_PrivateKey_file(ssl_ctx, config.keyfile.c_str(),
|
|
|
|
SSL_FILETYPE_PEM) != 1) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!config.certfile.empty()) {
|
|
|
|
if(SSL_CTX_use_certificate_chain_file(ssl_ctx,
|
|
|
|
config.certfile.c_str()) != 1) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
2012-06-25 17:05:12 +02:00
|
|
|
if (!SSL_set_tlsext_host_name(ssl, host.c_str())) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
if(ssl_handshake(ssl, fd) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-05-19 14:49:00 +02:00
|
|
|
spdySession.record_handshake_time();
|
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;
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
Spdylay sc(fd, ssl, spdy_version, callbacks, &spdySession);
|
|
|
|
spdySession.sc = ≻
|
2012-01-31 14:04:51 +01:00
|
|
|
|
|
|
|
nfds_t npollfds = 1;
|
|
|
|
pollfd pollfds[1];
|
|
|
|
|
2012-05-08 14:46:44 +02:00
|
|
|
if(spdy_version >= SPDYLAY_PROTO_SPDY3 && config.window_bits != -1) {
|
|
|
|
spdylay_settings_entry iv[1];
|
|
|
|
iv[0].settings_id = SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE;
|
|
|
|
iv[0].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
|
|
|
|
iv[0].value = 1 << config.window_bits;
|
|
|
|
int rv = sc.submit_settings(SPDYLAY_FLAG_SETTINGS_NONE, iv, 1);
|
|
|
|
assert(rv == 0);
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
for(int i = 0, n = spdySession.reqvec.size(); i < n; ++i) {
|
2012-08-21 15:02:35 +02:00
|
|
|
submit_request(sc, spdySession.hostport, config.headers,
|
|
|
|
spdySession.reqvec[i]);
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
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;
|
2012-04-27 18:07:36 +02:00
|
|
|
while(!sc.finish()) {
|
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)) {
|
2012-05-10 17:23:52 +02:00
|
|
|
int rv;
|
|
|
|
if((rv = sc.recv()) != 0 || (rv = sc.send()) != 0) {
|
2012-05-19 10:05:12 +02:00
|
|
|
if(rv != SPDYLAY_ERR_EOF || !spdySession.all_requests_processed()) {
|
2012-05-10 17:23:52 +02:00
|
|
|
std::cout << "Fatal: " << spdylay_strerror(rv) << std::endl;
|
2012-05-19 10:05:12 +02:00
|
|
|
std::cout << "reqnum=" << spdySession.reqvec.size()
|
|
|
|
<< ", completed=" << spdySession.complete << std::endl;
|
2012-05-10 17:23:52 +02:00
|
|
|
}
|
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) {
|
2012-05-19 10:05:12 +02:00
|
|
|
std::cout << "Requests to " << spdySession.hostport << "timed out.";
|
2012-04-04 19:19:00 +02:00
|
|
|
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
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
if(!spdySession.all_requests_processed()) {
|
|
|
|
std::cout << "Some requests were not processed. total="
|
|
|
|
<< spdySession.reqvec.size()
|
|
|
|
<< ", processed=" << spdySession.complete << std::endl;
|
|
|
|
}
|
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);
|
2012-05-19 14:49:00 +02:00
|
|
|
if(config.stat) {
|
|
|
|
print_stats(spdySession);
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
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-05-19 14:49:00 +02:00
|
|
|
callbacks.on_ctrl_recv_callback = on_ctrl_recv_callback2;
|
|
|
|
callbacks.on_ctrl_send_callback = on_ctrl_send_callback2;
|
2012-01-30 16:46:46 +01:00
|
|
|
if(config.verbose) {
|
2012-01-29 16:34:10 +01:00
|
|
|
callbacks.on_data_recv_callback = on_data_recv_callback;
|
2012-05-09 16:27:44 +02:00
|
|
|
callbacks.on_invalid_ctrl_recv_callback = on_invalid_ctrl_recv_callback;
|
2012-05-09 14:55:21 +02:00
|
|
|
callbacks.on_ctrl_recv_parse_error_callback =
|
|
|
|
on_ctrl_recv_parse_error_callback;
|
2012-05-09 15:41:08 +02:00
|
|
|
callbacks.on_unknown_ctrl_recv_callback = on_unknown_ctrl_recv_callback;
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
2012-05-19 10:05:12 +02: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::string prev_host;
|
|
|
|
uint16_t prev_port = 0;
|
2012-03-02 19:59:07 +01:00
|
|
|
int failures = 0;
|
2012-05-19 10:05:12 +02:00
|
|
|
SpdySession spdySession;
|
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) {
|
2012-05-19 10:05:12 +02:00
|
|
|
if(!spdySession.reqvec.empty()) {
|
|
|
|
spdySession.update_hostport();
|
|
|
|
if (communicate(prev_host, prev_port, spdySession, &callbacks) != 0) {
|
2012-03-02 19:59:07 +01:00
|
|
|
++failures;
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
spdySession = SpdySession();
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
|
|
|
prev_host = us.host;
|
|
|
|
prev_port = us.port;
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
spdySession.add_request(us);
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
if(!spdySession.reqvec.empty()) {
|
|
|
|
spdySession.update_hostport();
|
|
|
|
if (communicate(prev_host, prev_port, spdySession, &callbacks) != 0) {
|
2012-03-02 19:59:07 +01:00
|
|
|
++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-05-19 14:49:00 +02:00
|
|
|
out << "Usage: spdycat [-Oansv23] [-t <SECONDS>] [-w <WINDOW_BITS>] [--cert=<CERT>]\n"
|
2012-05-12 14:13:56 +02:00
|
|
|
<< " [--key=<KEY>] <URI>..."
|
2012-05-08 14:46:44 +02:00
|
|
|
<< 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-05-12 14:20:19 +02:00
|
|
|
<< " -2, --spdy2 Only use SPDY/2.\n"
|
2012-02-26 10:13:56 +01:00
|
|
|
<< " -3, --spdy3 Only use SPDY/3.\n"
|
2012-05-09 16:41:15 +02:00
|
|
|
<< " -t, --timeout=<N> Timeout each request after <N> seconds.\n"
|
2012-05-12 14:13:56 +02:00
|
|
|
<< " -w, --window-bits=<N>\n"
|
|
|
|
<< " Sets the initial window size to 2**<N>.\n"
|
2012-05-19 10:05:12 +02:00
|
|
|
<< " -a, --get-assets Download assets such as stylesheets, images\n"
|
|
|
|
<< " and script files linked from the downloaded\n"
|
|
|
|
<< " resource. Only links whose origins are the\n"
|
|
|
|
<< " same with the linking resource will be\n"
|
|
|
|
<< " downloaded.\n"
|
2012-05-19 14:49:00 +02:00
|
|
|
<< " -s, --stat Print statistics.\n"
|
2012-08-20 21:03:55 +02:00
|
|
|
<< " -H, --header Add a header to the requests.\n"
|
2012-05-09 16:41:15 +02:00
|
|
|
<< " --cert=<CERT> Use the specified client certificate file.\n"
|
2012-04-22 16:04:55 +02:00
|
|
|
<< " The file must be in PEM format.\n"
|
2012-05-09 16:41:15 +02:00
|
|
|
<< " --key=<KEY> Use the client private key file. The file\n"
|
2012-04-22 16:04:55 +02:00
|
|
|
<< " must be in PEM format.\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) {
|
2012-04-22 16:04:55 +02:00
|
|
|
int flag;
|
2012-01-30 16:46:46 +01:00
|
|
|
static option long_options[] = {
|
|
|
|
{"verbose", no_argument, 0, 'v' },
|
|
|
|
{"null-out", no_argument, 0, 'n' },
|
|
|
|
{"remote-name", no_argument, 0, 'O' },
|
2012-05-12 14:20:19 +02:00
|
|
|
{"spdy2", no_argument, 0, '2' },
|
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-05-08 14:46:44 +02:00
|
|
|
{"window-bits", required_argument, 0, 'w' },
|
2012-05-19 10:05:12 +02:00
|
|
|
{"get-assets", no_argument, 0, 'a' },
|
2012-05-19 14:49:00 +02:00
|
|
|
{"stat", no_argument, 0, 's' },
|
2012-04-22 16:04:55 +02:00
|
|
|
{"cert", required_argument, &flag, 1 },
|
|
|
|
{"key", required_argument, &flag, 2 },
|
2012-01-30 16:46:46 +01:00
|
|
|
{"help", no_argument, 0, 'h' },
|
2012-08-20 21:03:55 +02:00
|
|
|
{"header", required_argument, 0, 'H' },
|
2012-01-30 16:46:46 +01:00
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
int option_index = 0;
|
2012-08-20 21:03:55 +02:00
|
|
|
int c = getopt_long(argc, argv, "OanhH:v23st:w:", long_options,
|
2012-05-19 14:49:00 +02:00
|
|
|
&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-05-12 14:20:19 +02:00
|
|
|
case '2':
|
|
|
|
config.spdy_version = SPDYLAY_PROTO_SPDY2;
|
|
|
|
break;
|
2012-02-26 10:13:56 +01:00
|
|
|
case '3':
|
2012-05-12 14:20:19 +02:00
|
|
|
config.spdy_version = SPDYLAY_PROTO_SPDY3;
|
2012-02-26 10:13:56 +01:00
|
|
|
break;
|
2012-04-04 19:19:00 +02:00
|
|
|
case 't':
|
|
|
|
config.timeout = atoi(optarg);
|
|
|
|
break;
|
2012-05-08 14:46:44 +02:00
|
|
|
case 'w': {
|
|
|
|
errno = 0;
|
|
|
|
unsigned long int n = strtoul(optarg, 0, 10);
|
|
|
|
if(errno == 0 && n < 31) {
|
|
|
|
config.window_bits = n;
|
|
|
|
} else {
|
|
|
|
std::cerr << "-w: specify the integer in the range [0, 30], inclusive"
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-08-20 21:03:55 +02:00
|
|
|
case 'H': {
|
|
|
|
char *header = optarg;
|
|
|
|
char *value = strchr( optarg, ':' );
|
2012-08-21 14:39:33 +02:00
|
|
|
if ( ! value || header == value) {
|
2012-08-20 21:03:55 +02:00
|
|
|
std::cerr << "-H: invalid header: " << optarg
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
*value = 0;
|
|
|
|
value++;
|
|
|
|
while( isspace( *value ) ) { value++; }
|
|
|
|
if ( *value == 0 ) {
|
2012-08-21 15:02:35 +02:00
|
|
|
// This could also be a valid case for suppressing a header
|
|
|
|
// similar to curl
|
2012-08-20 21:03:55 +02:00
|
|
|
std::cerr << "-H: invalid header - value missing: " << optarg
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-08-21 15:02:35 +02:00
|
|
|
// Note that there is no processing currently to handle multiple
|
|
|
|
// message-header fields with the same field name
|
|
|
|
config.headers.insert(std::pair<std::string,std::string>(header, value));
|
2012-08-20 21:03:55 +02:00
|
|
|
}
|
2012-05-19 10:05:12 +02:00
|
|
|
case 'a':
|
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
config.get_assets = true;
|
|
|
|
#else // !HAVE_LIBXML2
|
|
|
|
std::cerr << "Warning: -a, --get-assets option cannot be used because\n"
|
|
|
|
<< "the binary was not compiled with libxml2."
|
|
|
|
<< std::endl;
|
|
|
|
#endif // !HAVE_LIBXML2
|
|
|
|
break;
|
2012-05-19 14:49:00 +02:00
|
|
|
case 's':
|
|
|
|
config.stat = true;
|
|
|
|
break;
|
2012-01-30 16:46:46 +01:00
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
2012-04-22 16:04:55 +02:00
|
|
|
case 0:
|
|
|
|
switch(flag) {
|
|
|
|
case 1:
|
|
|
|
// cert option
|
|
|
|
config.certfile = optarg;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// key option
|
|
|
|
config.keyfile = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-01-30 16:46:46 +01:00
|
|
|
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);
|
|
|
|
}
|