Exemplo n.º 1
0
        public HTTPResponse(
            string protocol,
            HttpStatusCode status,
            IDictionary <string, string> headers,
            string body)
        {
            this.Protocol     = protocol;
            this.Status       = (int)status;
            this.ReasonPhrase = HTTPUtils.GetReasonPhrase(this.Status);
            this.Headers      = new Dictionary <string, string>(headers, StringComparer.OrdinalIgnoreCase);
            var mem = new MemoryStream();

            using (var writer = new StreamWriter(mem)) {
                writer.Write(body);
            }
            this.Body = mem.ToArray();
            if (!Headers.ContainsKey("Content-Type"))
            {
                Headers.Add("Content-Type", "text/plain");
            }
            if (status == HttpStatusCode.Unauthorized && !Headers.ContainsKey("WWW-Authenticate"))
            {
                Headers.Add("WWW-Authenticate", "Basic realm=\"PeerCastStation\"");
            }
        }
Exemplo n.º 2
0
        protected override async Task DoProcess(CancellationToken cancellationToken)
        {
            this.state = ConnectionState.Waiting;
            try {
                if (connection != null && !IsStopped)
                {
                    await Handshake(cancellationToken).ConfigureAwait(false);
                    await ReadContents(cancellationToken).ConfigureAwait(false);
                }
                this.state = ConnectionState.Closed;
            }
            catch (HTTPError e) {
                await connection.Stream.WriteUTF8Async(HTTPUtils.CreateResponseHeader(e.StatusCode)).ConfigureAwait(false);

                Stop(StopReason.BadAgentError);
                this.state = ConnectionState.Error;
            }
            catch (IOException e) {
                Logger.Error(e);
                Stop(StopReason.ConnectionError);
                this.state = ConnectionState.Error;
            }
            catch (ConnectionStoppedExcception) {
                this.state = ConnectionState.Closed;
            }
        }
Exemplo n.º 3
0
        protected override async Task <StopReason> DoProcess(CancellationToken cancel_token)
        {
            try {
                if (!HTTPUtils.CheckAuthorization(request, AccessControlInfo))
                {
                    throw new HTTPError(HttpStatusCode.Unauthorized);
                }
                await WaitChannelReceived().ConfigureAwait(false);
                await SendResponseHeader().ConfigureAwait(false);

                switch (RequestMode)
                {
                case RequestType.WMSPDescribe:
                    await SendReponseBodyHeaderOnly(cancel_token).ConfigureAwait(false);

                    break;

                case RequestType.HttpGet:
                case RequestType.WMSPPlay:
                default:
                    await SendReponseBody(cancel_token).ConfigureAwait(false);

                    break;
                }
                return(StopReason.OffAir);
            }
            catch (HTTPError err) {
                await SendErrorResponse(err.StatusCode).ConfigureAwait(false);

                return(StopReason.OffAir);
            }
        }
Exemplo n.º 4
0
        private async Task SendErrorResponse(HttpStatusCode code)
        {
            var response_header = HTTPUtils.CreateResponseHeader(code, new Dictionary <string, string>());
            await Connection.WriteAsync(System.Text.Encoding.UTF8.GetBytes(response_header)).ConfigureAwait(false);

            Logger.Debug("Header: {0}", response_header);
        }
Exemplo n.º 5
0
 public HTTPResponse(
     string protocol,
     int status,
     string reason_phrase,
     IDictionary <string, string> headers,
     byte[] body)
 {
     this.Protocol     = protocol;
     this.Status       = status;
     this.ReasonPhrase = reason_phrase ?? HTTPUtils.GetReasonPhrase(status);
     this.Headers      = new Dictionary <string, string>(headers, StringComparer.OrdinalIgnoreCase);
     this.Body         = body;
 }
Exemplo n.º 6
0
 public HTTPResponse(
     string protocol,
     HttpStatusCode status)
 {
     this.Protocol     = protocol;
     this.Status       = (int)status;
     this.ReasonPhrase = HTTPUtils.GetReasonPhrase(this.Status);
     this.Headers      = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
     this.Body         = null;
     if (status == HttpStatusCode.Unauthorized && !Headers.ContainsKey("WWW-Authenticate"))
     {
         Headers.Add("WWW-Authenticate", "Basic realm=\"PeerCastStation\"");
     }
 }
Exemplo n.º 7
0
        private async Task SendPlaylist(CancellationToken cancel_token)
        {
            Logger.Debug("Sending Playlist");
            var pls = CreatePlaylist();

            pls.Channels.Add(Channel);
            var baseuri = new Uri(
                new Uri(request.Uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped)),
                "stream/");

            if (AccessControlInfo.AuthenticationKey != null)
            {
                var parameters = new Dictionary <string, string>()
                {
                    { "auth", HTTPUtils.CreateAuthorizationToken(AccessControlInfo.AuthenticationKey) },
                };
                await Connection.WriteAsync(pls.CreatePlayList(baseuri, parameters), cancel_token).ConfigureAwait(false);
            }
            else
            {
                await Connection.WriteAsync(pls.CreatePlayList(baseuri, Enumerable.Empty <KeyValuePair <string, string> >()), cancel_token).ConfigureAwait(false);
            }
        }