ToString() public method

public ToString ( ) : string
return string
示例#1
0
 public void DefaultPropertyValues_ReturnEmptyAfterConstruction_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     Assert.Equal(0, w.AllKeys.Length);
     Assert.Equal(0, w.Count);
     Assert.Equal("\r\n", w.ToString());
 }
		public void ToStringTest ()
		{
			col.Add ("Name1", "Value1b");
			col.Add ("Name3", "Value3a\r\n Value3b");
			col.Add ("Name4", "   Value4   ");
			Assert.AreEqual ("Name1: Value1,Value1b\r\nName2: Value2\r\nName3: Value3a\r\n Value3b\r\nName4: Value4\r\n\r\n", col.ToString (), "#1");
			WebHeaderCollection w;
			w = new WebHeaderCollection ();
			w.Add (HttpResponseHeader.KeepAlive, "Value1");
			w.Add (HttpResponseHeader.WwwAuthenticate, "Value2");
			Assert.AreEqual ("Keep-Alive: Value1\r\nWWW-Authenticate: Value2\r\n\r\n", w.ToString (), "#2");
			w = new WebHeaderCollection ();
			w.Add (HttpRequestHeader.UserAgent, "Value1");
			w.Add (HttpRequestHeader.ContentMd5, "Value2");
			Assert.AreEqual ("User-Agent: Value1\r\nContent-MD5: Value2\r\n\r\n", w.ToString (), "#3");
		}
示例#3
0
 public void ToString_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("Accept", "text/plain");
     w.Add("Content-Length", "123");
     Assert.Equal(
         "Accept: text/plain\r\nContent-Length: 123\r\n\r\n",
         w.ToString());
 }
示例#4
0
		string GetHeaders ()
		{
			bool continue100 = false;
			if (sendChunked) {
				continue100 = true;
				webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
				webHeaders.RemoveInternal ("Content-Length");
			} else if (contentLength != -1) {
				if (ntlm_auth_state != NtlmAuthState.Challenge) {
					if (contentLength > 0)
						continue100 = true;

					webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
				} else {
					webHeaders.SetInternal ("Content-Length", "0");
				}
				webHeaders.RemoveInternal ("Transfer-Encoding");
			} else {
				webHeaders.RemoveInternal ("Content-Length");
			}

			if (actualVersion == HttpVersion.Version11 && continue100 &&
			    servicePoint.SendContinue) { // RFC2616 8.2.3
				webHeaders.RemoveAndAdd ("Expect" , "100-continue");
				expectContinue = true;
			} else {
				webHeaders.RemoveInternal ("Expect");
				expectContinue = false;
			}

			bool proxy_query = ProxyQuery;
			string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
			webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
			Version proto_version = servicePoint.ProtocolVersion;
			bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);

			if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
				webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
			} else if (!keepAlive && version == HttpVersion.Version11) {
				webHeaders.RemoveAndAdd (connectionHeader, "close");
			}

			webHeaders.SetInternal ("Host", Host);
			if (cookieContainer != null) {
				string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
				if (cookieHeader != "")
					webHeaders.RemoveAndAdd ("Cookie", cookieHeader);
				else
					webHeaders.RemoveInternal ("Cookie");
			}

			string accept_encoding = null;
			if ((auto_decomp & DecompressionMethods.GZip) != 0)
				accept_encoding = "gzip";
			if ((auto_decomp & DecompressionMethods.Deflate) != 0)
				accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
			if (accept_encoding != null)
				webHeaders.RemoveAndAdd ("Accept-Encoding", accept_encoding);

			if (!usedPreAuth && preAuthenticate)
				DoPreAuthenticate ();

			return webHeaders.ToString ();
		}
示例#5
0
        internal void SendHeaders(bool closing, MemoryStream ms)
        {
            //TODO: When do we send KeepAlive?
            Encoding encoding = content_encoding;

            if (encoding == null)
            {
                encoding = Encoding.Default;
            }

            if (content_type != null)
            {
                if (content_encoding != null && content_type.IndexOf("charset=") == -1)
                {
                    string enc_name = content_encoding.WebName;
                    headers.SetInternal("Content-Type", content_type + "; charset=" + enc_name);
                }
                else
                {
                    headers.SetInternal("Content-Type", content_type);
                }
            }

            if (headers ["Server"] == null)
            {
                headers.SetInternal("Server", "Mono-HTTPAPI/1.0");
            }

            CultureInfo inv = CultureInfo.InvariantCulture;

            if (headers ["Date"] == null)
            {
                headers.SetInternal("Date", DateTime.UtcNow.ToString("r", inv));
            }

            if (!chunked)
            {
                if (!cl_set && closing)
                {
                    cl_set         = true;
                    content_length = 0;
                }

                if (cl_set)
                {
                    headers.SetInternal("Content-Length", content_length.ToString(inv));
                }
            }

            Version v = context.Request.ProtocolVersion;

            if (!cl_set && !chunked && v >= HttpVersion.Version11)
            {
                chunked = true;
            }

            /* Apache forces closing the connection for these status codes:
             *	HttpStatusCode.BadRequest       400
             *	HttpStatusCode.RequestTimeout       408
             *	HttpStatusCode.LengthRequired       411
             *	HttpStatusCode.RequestEntityTooLarge    413
             *	HttpStatusCode.RequestUriTooLong    414
             *	HttpStatusCode.InternalServerError  500
             *	HttpStatusCode.ServiceUnavailable   503
             */
            bool conn_close = (status_code == 400 || status_code == 408 || status_code == 411 ||
                               status_code == 413 || status_code == 414 || status_code == 500 ||
                               status_code == 503);

            if (conn_close == false)
            {
                conn_close  = (context.Request.Headers ["connection"] == "close");
                conn_close |= (v <= HttpVersion.Version10);
            }

            // They sent both KeepAlive: true and Connection: close!?
            if (!keep_alive || conn_close)
            {
                headers.SetInternal("Connection", "close");
            }

            if (chunked)
            {
                headers.SetInternal("Transfer-Encoding", "chunked");
            }

            int chunked_uses = context.Connection.ChunkedUses;

            if (chunked_uses >= 100)
            {
                force_close_chunked = true;
                if (!conn_close)
                {
                    headers.SetInternal("Connection", "close");
                }
            }

            if (location != null)
            {
                headers.SetInternal("Location", location);
            }

            if (cookies != null)
            {
                bool          firstDone = false;
                StringBuilder cookieSB  = new StringBuilder();
                foreach (Cookie cookie in cookies)
                {
                    if (firstDone)
                    {
                        cookieSB.Append(",");
                    }
                    firstDone = true;
                    cookieSB.Append(cookie.ToClientString());
                }
                headers.SetInternal("Set-Cookie2", cookieSB.ToString());
            }

            StreamWriter writer = new StreamWriter(ms, encoding);

            writer.Write("HTTP/{0} {1} {2}\r\n", version, status_code, status_description);
            string headers_str = headers.ToString();

            writer.Write(headers_str);
            writer.Flush();
            int preamble = encoding.GetPreamble().Length;

            if (output_stream == null)
            {
                output_stream = context.Connection.GetResponseStream();
            }

            /* Assumes that the ms was at position 0 */
            ms.Position = preamble;
            HeadersSent = true;
        }
示例#6
0
        string GetHeaders()
        {
            bool continue100 = false;

            if (contentLength != -1)
            {
                if (contentLength > 0)
                {
                    continue100 = true;
                }
                webHeaders.SetInternal("Content-Length", contentLength.ToString());
                webHeaders.RemoveInternal("Transfer-Encoding");
            }
            else if (sendChunked)
            {
                continue100 = true;
                webHeaders.RemoveAndAdd("Transfer-Encoding", "chunked");
                webHeaders.RemoveInternal("Content-Length");
            }

            if (actualVersion == HttpVersion.Version11 && continue100 &&
                servicePoint.SendContinue)               // RFC2616 8.2.3
            {
                webHeaders.RemoveAndAdd("Expect", "100-continue");
                expectContinue = true;
            }
            else
            {
                webHeaders.RemoveInternal("Expect");
                expectContinue = false;
            }

            string connectionHeader = (ProxyQuery) ? "Proxy-Connection" : "Connection";

            webHeaders.RemoveInternal((!ProxyQuery) ? "Proxy-Connection" : "Connection");
            bool spoint10 = (servicePoint.ProtocolVersion == null ||
                             servicePoint.ProtocolVersion == HttpVersion.Version10);

            if (keepAlive && (version == HttpVersion.Version10 || spoint10))
            {
                webHeaders.RemoveAndAdd(connectionHeader, "keep-alive");
            }
            else if (!keepAlive && version == HttpVersion.Version11)
            {
                webHeaders.RemoveAndAdd(connectionHeader, "close");
            }

            webHeaders.SetInternal("Host", actualUri.Authority);
            if (cookieContainer != null)
            {
                string cookieHeader = cookieContainer.GetCookieHeader(actualUri);
                if (cookieHeader != "")
                {
                    webHeaders.SetInternal("Cookie", cookieHeader);
                }
            }

            if (!usedPreAuth && preAuthenticate)
            {
                DoPreAuthenticate();
            }

            return(webHeaders.ToString());
        }