New test.
[mono.git] / mcs / class / System.Web / Test / standalone / response / rtest.cs
1 //
2 // Usage:
3 //    rtest host port url 
4 //
5 // This program dumps the results of the HTTP request without any HTTP
6 // headers
7 //
8 using System;
9 using System.Net;
10 using System.IO;
11 using System.Net.Sockets;
12
13 class X {
14         static NetworkStream ns;
15         static StreamWriter sw;
16         static StreamReader sr;
17         static TcpClient c;
18         static bool debug;
19         static bool headers;
20         static string header;
21         
22         static void send (string s)
23         {
24                 if (debug)
25                         Console.WriteLine (s);
26                 
27                 sw.Write (s);
28                 sw.Flush ();
29         }
30         
31         static void Main (string [] args)
32         {
33                 int i = 0;
34
35                 while (args [i].StartsWith ("-")){
36                         if (args [i] == "-debug")
37                                 debug = true;
38                         if (args [i] == "-headers")
39                                 headers = true;
40                         if (args [i] == "-header")
41                                 header = args [++i];
42                         i++;
43                 }
44                 
45                 c = new TcpClient (args [i], Int32.Parse (args [i+1]));
46                 c.ReceiveTimeout = 1000;
47                 ns = c.GetStream ();
48                 
49                 sw = new StreamWriter (ns);
50                 sr = new StreamReader (ns);
51
52                 string host = args [i];
53                 if (args [i+1] != "80")
54                         host += ":" + args [i+1];
55                 send (String.Format ("GET {0} HTTP/1.1\r\nHost: {1}\r\n\r\n", args [i+2], host));
56
57                 MemoryStream ms = new MemoryStream ();
58                 
59                 try {
60                         byte [] buf = new byte [1024];
61                         int n;
62                         
63                         while ((n = ns.Read (buf, 0, 1024)) != 0){
64                                 ms.Write (buf, 0, n);
65                         }
66                 } catch {}
67
68                 ms.Position = 0;
69                 sr = new StreamReader (ms);
70
71                 string s;
72                 
73                 while ((s = sr.ReadLine ()) != null){
74                         if (s == ""){
75                                 if (headers)
76                                         return;
77                                 
78                                 string x = sr.ReadToEnd ();
79                                 Console.Write (x);
80                                 break;
81                         }  else {
82                                 if (debug || headers)
83                                         Console.WriteLine (s);
84                                 if (header != null && s.StartsWith (header)){
85                                         Console.WriteLine (s);
86                                         return;
87                                 }
88                         }
89                 }
90         }
91 }
92