Exemplo n.º 1
0
        private void Respond(Socket socket, StateObject state)
        {
            if (!socket.Connected || !running)
            {
                return;
            }

            var requestInfo = new RequestInfo(state.buffer);

            Trace.WriteLine("Requested:\t" + requestInfo.Url);

            ResponseInfo responseInfo = null;
            WebContent   content      = null;

            if (!requestInfo.Success)
            {
                content = NotFoundContent(requestInfo, out responseInfo);
            }
            else
            {
                if (ContentGetter != null)
                {
                    content = ContentGetter(requestInfo.Url);
                }

                if (content == null)
                {
                    content = NotFoundContent(requestInfo, out responseInfo);
                }
                else
                {
                    responseInfo = content.Respond(requestInfo);
                    if (!responseInfo.Success)
                    {
                        content = NotFoundContent(requestInfo, out responseInfo);
                    }
                }
            }
            var header =
                MakeHeader(requestInfo.HttpVersion, responseInfo.MimeType, responseInfo.Data.Length, responseInfo.StatusCode);

            byte[] buffer = new Byte[header.Length + responseInfo.Data.Length];
            Buffer.BlockCopy(header, 0, buffer, 0, header.Length);
            Buffer.BlockCopy(responseInfo.Data, 0, buffer, header.Length, responseInfo.Data.Length);
            if (Asycn)
            {
                try {
                    if (socket.Connected)
                    {
                        socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(RespondCallback), socket);
                    }
                } catch (Exception ex) {
                    Debug.WriteLine("Error in webserver-respond:" + ex.Message);
                }
            }
            else
            {
                if (socket.Connected && running)
                {
                    int bytesSent = socket.Send(buffer);
                    Trace.WriteLine("Sent " + bytesSent + " bytes to client.");

                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                }
            }
        }
Exemplo n.º 2
0
        public override ResponseInfo Respond(RequestInfo requestInfo)
        {
            var result = new ResponseInfo();

            result.Success = false;
            if (requestInfo.Uri == null)
            {
                return(result);
            }

            lock (FailedConnections) {
                if (FailedConnections.Contains(requestInfo.Uri.AbsoluteUri))
                {
                    return(result);
                }
            }

            var webRequest = WebRequest.CreateDefault(requestInfo.Uri);

            System.Net.WebResponse webResponse = null;
            try {
                webResponse = webRequest.GetResponse();

                var contentLength  = webResponse.ContentLength;
                var responseStream = webResponse.GetResponseStream();

                if (contentLength > 0)
                {
                    int pos = 0;

                    result.Data = new byte[contentLength];

                    int bytesRead = 1;
                    while (pos < contentLength && bytesRead != 0)
                    {
                        bytesRead = responseStream.Read(result.Data, pos, (int)contentLength - pos);
                        pos      += bytesRead;
                    }

                    if (pos == contentLength)
                    {
                        result.Success = true;
                    }
                    else
                    {
                        result.Success = false;
                    }
                }
                else
                {
                    var stream        = new MemoryStream();
                    var bytesReceived = 1;
                    var totalReceived = 0;
                    var buff          = new byte[1024];
                    while (bytesReceived != 0)
                    {
                        bytesReceived  = responseStream.Read(buff, 0, buff.Length);
                        totalReceived += bytesReceived;
                        stream.Write(buff, 0, bytesReceived);
                    }
                    result.Data = new byte[stream.Length];
                    // GetBuffer gives back the whole buffer, that is more than stream.Lenght
                    Array.Copy(stream.GetBuffer(), 0, result.Data, 0, stream.Length);
                    stream.Close();
                    result.Success = true;
                }
                responseStream.Close();

                result.MimeType = webResponse.ContentType;
            } catch (WebException we) {
                result.Success = false;
                lock (FailedConnections) {
                    FailedConnections.Add(requestInfo.Uri.AbsoluteUri);
                }
            } finally {
                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
            return(result);
        }