Пример #1
0
        /// <summary>
        /// 发送头
        /// </summary>
        /// <param name="sHttpVersion"></param>
        /// <param name="sMIMEHeader"></param>
        /// <param name="iTotBytes"></param>
        /// <param name="sStatusCode"></param>
        private string CreateHeader(string sHttpVersion, RequestInfo request, ResponseInfo resInfo, long iTotBytes, string sStatusCode, Encoding encoding)
        {
            StringBuilder sBuffer = new StringBuilder();


            sBuffer.AppendLine("HTTP/1.1 " + resInfo.StatusCode + " OK");
            if (string.IsNullOrEmpty(resInfo.MimeType))
            {
                resInfo.MimeType = "text/html"; // 默认 text/html
            }

            sBuffer.AppendLine(sHttpVersion + sStatusCode);
            if (resInfo.MimeType.Equals("text/html", StringComparison.CurrentCultureIgnoreCase))
            {
                sBuffer.AppendLine("Content-Type: " + resInfo.MimeType + ";charset=" + encoding.WebName);
                sBuffer.AppendLine("Content-Encoding: " + encoding.WebName);
            }
            if (!resInfo.Header.ContainsKey("Accept-Ranges"))
            {
                sBuffer.AppendLine("Accept-Ranges: none");
            }

            //if (request.Header.ContainsKey("range"))
            //{
            //    resInfo.Header["Content-Range"] = request.Header["range"].Replace('=', ' ') + "/" + iTotBytes;
            //}

            if (!resInfo.Header.ContainsKey("Server"))
            {
                resInfo.Header["Server"] = "Buffalo Mini Server";
            }
            if (!resInfo.Header.ContainsKey("Content-Length"))
            {
                resInfo.Header["Content-Length"] = iTotBytes.ToString();
            }

            foreach (KeyValuePair <string, string> kvp in resInfo.Header)
            {
                sBuffer.AppendLine(kvp.Key + ": " + kvp.Value);
            }
            sBuffer.AppendLine("");
            return(sBuffer.ToString());
        }
Пример #2
0
        private void DelSocket(object prm)
        {
            Socket requestSocket = prm as Socket;

            if (requestSocket == null)
            {
                return;
            }
            try
            {
                byte[] receiveContent = null;//接收的内容
                if (requestSocket.Connected)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        Byte[] bufferContent = new Byte[1024];
                        int    receiveBytes  = 0;
                        do
                        {
                            receiveBytes = requestSocket.Receive(bufferContent, bufferContent.Length, 0);
                            ms.Write(bufferContent, 0, receiveBytes);
                        } while (receiveBytes >= bufferContent.Length);
                        if (ms.Length <= 0)
                        {
                            return;
                        }
                        receiveContent = ms.ToArray();
                    }
                }
                else
                {
                    return;
                }
                requestSocket.SendTimeout = 5000;
                string      content = DefaultEncoding.GetString(receiveContent);
                RequestInfo info    = new RequestInfo(content);
                if (OnRequestProcessing != null)
                {
                    Encoding encoding = GetEncoding(info);
                    using (ResponseInfo resInfo = new ResponseInfo(encoding))
                    {
                        OnRequestProcessing(info, resInfo);
                        byte[] rcontent = resInfo.ResponseContent;
                        long   len      = resInfo.Length;
                        if (len == 0)
                        {
                            len = rcontent.Length;
                        }


                        string header      = CreateHeader(info.HttpVersion, info, resInfo, len, " 200 OK", encoding);
                        byte[] headContent = encoding.GetBytes(header);

                        requestSocket.Send(headContent);

                        if (OnRequestSendingBody != null)
                        {
                            OnRequestSendingBody(info, resInfo, requestSocket);
                        }
                        if (rcontent.Length > 0)
                        {
                            requestSocket.Send(rcontent);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
            finally
            {
                try
                {
                    requestSocket.Close();
                }
                catch { }
            }
        }