[Mono-list] Patch: HttpWebRequest and header handling

Tim Haynes thaynes@openlinksw.com (Tim Haynes)
Wed, 23 Oct 2002 10:05:34 +0100


--=-=-=

Howdo,

Please find attached another patch:

ChangeLog:
- Avoided double-sending of the request to the server when both
  GetRequestStream() and GetResponse() are called.
- System Header attributes survive the Headers attribute set.
- Added handling of Connection: KeepAlive/Close

Also attached is an example to send SMSes via redcoal.

Cheers,

~Tim
-- 
Product Development Consultant
OpenLink Software
Tel: +44 (0) 20 8681 7701
Web: <http://www.openlinksw.com>
Universal Data Access & Data Integration Technology Providers


--=-=-=
Content-Disposition: attachment; filename=redcoal.cs
Content-Description: sample to send SMSs via redcoal.net

using System;
using System.Xml;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace redcoalsms_dom
{
  public class redcoalsmssvc
    {
      string ClientSerialNo, SMSKey, strSenderName, strSenderEmail;
      bool canReply;

      static string [] error_responces = 
	{
	  "No Error",
	  "Feature Not Available", 
	  "Service Not Available",
	  "Too Many Wrong Passwords , Please contact support@redcoal.com",
	  "Invalid password",
	  "No Credits Left/ go to: http://www.redcoal.net/purchase.asp",
	  "Not Enough Credits Left",
	  "Binary File Not Found",
	  "One or more invalid destinations",
	  "Invalid Format (for binary and fax data)",
	  "Invalid Serial No",
	  "Invalid HTTP property",
	  "Daily Quota Reached",
	  "Destination not in restricted list",
	  "Invalid File",
	  "File too big",
	  "General Fault: E.g: no internet connection, can't connect to Redcoal XML server, can't get past the proxy firewall.",
	  "Can not read the specified file or don't have permission to read the file"
	};
      public redcoalsmssvc (string _ClientSerialNo, string _SMSKey, string _SenderName, string _SenderEmail)
	{
	  this.canReply = false;
	  this.ClientSerialNo = _ClientSerialNo;
	  this.strSenderEmail = _SenderEmail;
	  this.strSenderName = _SenderName;
	  this.SMSKey = _SMSKey;
	}

      private static string RC_req_fmt = 
	  " <SOAP-ENV:Envelope " +
	  "   xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' " +
	  "   xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' " +
	  "   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
	  "   xmlns:xsd='http://www.w3.org/2001/XMLSchema' " +
	  "   SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" +
	  "     <SOAP-ENV:Body>" +
	  "         <m:SendTextSMS " +
	  "           xmlns:m='http://tempuri.org/message/'>" +
	  "             <ClientSerialNo xsi:type='xsd:string'>{0}</ClientSerialNo>" +
	  "             <txtSMSKey xsi:type='xsd:string'>{1}</txtSMSKey>" +
	  "             <strReceipients xsi:type='xsd:string'>{2}</strReceipients>" +
	  "             <strSenderEmail xsi:type='xsd:string'>{3}</strSenderEmail>" +
	  "             <strSenderName xsi:type='xsd:string'>{4}</strSenderName>" +
	  "             <txtMsg xsi:type='xsd:string'>{5}</txtMsg>" +
	  "             <canReply xsi:type='xsd:boolean'>{6}</canReply>" +
	  "         </m:SendTextSMS>" +
	  "     </SOAP-ENV:Body>" +
	  " </SOAP-ENV:Envelope>";
      private static string req_uri = "http://xml.redcoal.net/smssoap/xmlserver.ASP";
      private static string SoapAction = "\"http://tempuri.org/action/SMSServer.SendTextSMS\"";
      private static string SoapResp_uri = "http://tempuri.org/message/";
      private static string SoapResp_name = "SendTextSMSResponse";
      private static string SoapResp_val = "Result";

      private int SendTextSMS (string ClientSerialNo,
	  string SMSKey, 
	  string Recepient,
	  string strSenderEmail, 
	  string strSenderName,
	  string txtMsg,
	  bool canReply)
	{
	  Object[] obj_params = { ClientSerialNo, SMSKey, Recepient, strSenderEmail, strSenderName, txtMsg, canReply };
	  WebHeaderCollection req_headers = new WebHeaderCollection();
	  string req_text = String.Format (RC_req_fmt, obj_params);

	  byte [] req_bytes = new UTF8Encoding().GetBytes (req_text);

	  req_text = null;
	  Uri _req_uri = new Uri (req_uri);
	  HttpWebRequest web_req = (HttpWebRequest) WebRequest.Create (_req_uri);
	  req_headers.Add ("SOAPAction", SoapAction);
	  web_req.Headers =  req_headers;
	  web_req.ContentType = "text/xml";
	  web_req.KeepAlive = false;
	  web_req.UserAgent = "Virtuoso SOAP sample";
	  web_req.Method = "POST";
	  web_req.ContentLength = req_bytes.Length;

	  Stream req_str = web_req.GetRequestStream ();
	  req_str.Write (req_bytes, 0, req_bytes.Length);
	  req_str.Flush();
	  req_bytes = null;
	  req_str = null;

	  HttpWebResponse resp = (HttpWebResponse) web_req.GetResponse ();
	  Encoding enc = new UTF8Encoding ();

	  XmlDocument doc = new XmlDocument ();

	  long clen = resp.ContentLength;
	  Stream resp_stream = resp.GetResponseStream();
	  String content;
	  if (clen != -1)
	    {
	      byte [] bytes = new byte[clen];
	      long read = 0;
	      while (read < clen)
		{
		  read += resp_stream.Read (bytes, (int) read, (int) (clen - read));
		}
	      UTF8Encoding enc1 = new UTF8Encoding  (false, true);
	      content = new String (enc1.GetChars (bytes));
	    }
	  else if (typeof (NetworkStream).IsInstanceOfType (resp_stream))
	    {
	      NetworkStream nstr = resp_stream as NetworkStream;
	      byte [] bytes = new byte[10000];
	      long read = 0;
	      while (nstr.DataAvailable)
		{
		  read += nstr.Read (bytes, (int) read,  (int) (bytes.Length - read));
		}
	      UTF8Encoding enc1 = new UTF8Encoding  (false, true);
	      content = new String (enc1.GetChars (bytes, 0, (int) read));
	    }
	  else 
	    throw new Exception ("don't know how to read the stream");
	  resp_stream.Close();
	  doc.LoadXml (content);

	  XmlNode xml_env = doc.DocumentElement;
	  XmlNode xml_body = xml_env["Body", xml_env.NamespaceURI];
	  XmlNode xml_fault = xml_body["Fault", xml_env.NamespaceURI];
	  if (xml_fault != null)
	      throw new Exception (xml_fault.InnerText);

	  XmlNode xml_resp = xml_body[SoapResp_name, SoapResp_uri];
	  XmlNode result = xml_resp[SoapResp_val];
	  return Int32.Parse (result.InnerXml);
	}


      public string SendSms (string Recepient, string txtMsg)
	{
	  int resp = SendTextSMS (
	      ClientSerialNo, SMSKey, Recepient, 
	      strSenderEmail, strSenderName, txtMsg, canReply);
	  if (resp >= 0 && resp <= 17)
	    return error_responces[resp];
	  else
	    return "Unknown status code " + resp;
	}

      public static void Main (String [] args)
	{
	  redcoalsmssvc srv = new redcoalsmssvc ("94765-49251-11951-66295", "xxx", String.Empty, String.Empty);
	  Console.WriteLine (srv.SendSms ("+123", "ha-ha"));
	}

    }
}

--=-=-=
Content-Disposition: attachment; filename=http.diff
Content-Description: patch for HttpWebRequest

Index: class/System/System.Net/HttpWebRequest.cs
===================================================================
RCS file: /mono/mcs/class/System/System.Net/HttpWebRequest.cs,v
retrieving revision 1.8
diff -u -b -r1.8 HttpWebRequest.cs
--- class/System/System.Net/HttpWebRequest.cs	15 Oct 2002 00:24:33 -0000	1.8
+++ class/System/System.Net/HttpWebRequest.cs	22 Oct 2002 17:05:24 -0000
@@ -190,6 +190,10 @@
 				int count = value.Count;
 				for (int i = 0; i < count; i++) 
 					newHeaders.Add (value.GetKey (i), value.Get (i));
+				newHeaders.SetInternal ("Host", this.webHeaders["Host"]);
+				newHeaders.SetInternal ("Date", this.webHeaders["Date"]);
+				newHeaders.SetInternal ("Expect", this.webHeaders["Expect"]);
+				newHeaders.SetInternal ("Connection", this.webHeaders["Connection"]);
 				webHeaders = newHeaders;
 			}
 		}
@@ -222,6 +226,8 @@
 			set {
 				CheckRequestStarted ();
 				keepAlive = value;
+				if (Connection == null)
+				  webHeaders.SetInternal ("Connection", value ? "Keep-Alive" : "Close");
 			}
 		}
 		
@@ -451,6 +457,7 @@
 		
 		internal Stream GetRequestStreamInternal ()
 		{
+		        if (this.requestStream == null)
 			this.requestStream = new HttpWebStream (this);
 			return this.requestStream;
 		}
@@ -504,16 +511,14 @@
 		{
 			if (webResponse != null)
 				return webResponse;			
-			lock (this) {
-				if (requesting) {
-					requestEndEvent = new AutoResetEvent (false);
-				}
-			}
-			if (requestEndEvent != null) {
-				requestEndEvent.WaitOne ();
-			}
-			Stream responseStream = new HttpWebStream (this);
+
+			Stream responseStream = this.requestStream == null ? 
+			    new HttpWebStream (this) : this.requestStream;
+			do
+			  {
  			this.webResponse = new HttpWebResponse (this.actualUri, method, responseStream);
+			  }
+			while (this.webResponse.StatusCode == HttpStatusCode.Continue);
  			return (WebResponse) this.webResponse;
 		}
 

--=-=-=--