1 //
2 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 //   Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 
19 #ifdef HAVE_CONFIG_H
20 #include "gnashconfig.h"
21 #endif
22 
23 #include "NetworkAdapter.h"
24 #include "tu_file.h"
25 #include "IOChannel.h"
26 
27 #include <memory>               // for unique_ptr
28 #include <cstdio>
29 #include <iostream>
30 #include <cassert>
31 
32 using namespace std;
33 
34 const char* post = NULL;
35 
36 #define CHUNK_SIZE 4
37 
38 static void
dump_curl(const char * url,ostream & os)39 dump_curl(const char* url, ostream& os)
40 {
41 	std::unique_ptr<gnash::IOChannel> reader;
42 	if ( post )
43 	{
44 		reader.reset( gnash::NetworkAdapter::make_stream(url, post) );
45 	}
46 	else
47 	{
48 		reader.reset( gnash::NetworkAdapter::make_stream(url) );
49 	}
50 
51 	assert(reader.get());
52 
53 	char buf[CHUNK_SIZE];
54 
55 	while (size_t read = reader->read(buf, CHUNK_SIZE) )
56 	{
57 		for (size_t i=0; i<read; i++) {
58 			os << buf[i];
59 		}
60 	}
61 
62 }
63 
64 static void
dump_tu_file(const char * url,ostream & os)65 dump_tu_file(const char* url, ostream& os)
66 {
67     std::unique_ptr<IOChannel> reader = makeFileChannel(url, true);
68 	assert(reader);
69 	if (reader->bad()) return;
70 
71 	char buf[CHUNK_SIZE];
72 
73 	while ( size_t read = reader->read(buf, CHUNK_SIZE) )
74 	{
75 		for (size_t i=0; i<read; i++) {
76 			os << buf[i];
77 		}
78 	}
79 
80 	if ( reader->eof() )
81 		printf("-EOF-\n");
82 
83 }
84 
85 static void
dump_file(const char * url,ostream & os)86 dump_file(const char* url, ostream& os)
87 {
88 	FILE* f = fopen(url, "r");
89 	if (!f) return;
90 
91 	char buf[CHUNK_SIZE];
92 
93 	while ( size_t read=fread(buf, 1, CHUNK_SIZE, f) )
94 	{
95 		for (size_t i=0; i<read; i++) {
96 			os << buf[i];
97 		}
98 	}
99 	fclose(f);
100 }
101 
102 int
main(int argc,char ** argv)103 main(int argc, char** argv)
104 {
105 	const char* input = INPUT; // Should be the path to this file
106 
107 	if ( argc == 1 )
108 	{
109 		cerr << "Usage: " << argv[0] << " <url> [<postdata>]" << endl;
110 		exit(EXIT_FAILURE);
111 	}
112 
113 	if ( argc > 1 ) input = argv[1];
114 
115 	if ( argc > 2 ) post = argv[2];
116 
117 	cout << "input: " << input << endl;
118 	if ( post ) cout << "post data: " << post << endl;
119 
120 #if 0
121 	cout << "FILE" << endl;
122 	dump_file(input, cout);
123 	cout << "TU_FILE" << endl;
124 	dump_tu_file(input, cout);
125 #endif
126 	cout << "CURL" << endl;
127 	dump_curl(input, cout);
128 
129 	return 0;
130 }
131 
132