public ItemResponse(IRequest request, IMediaResource aItem, string transferMode = "Streaming") { item = aItem; headers = new ResponseHeaders(noCache: !(item is IMediaCoverResource)); var meta = item as IMetaInfo; if (meta != null) { headers.Add("Content-Length", meta.InfoSize.ToString()); headers.Add("Last-Modified", meta.InfoDate.ToString("R")); } headers.Add("Accept-Ranges", "bytes"); headers.Add("Content-Type", DlnaMaps.Mime[item.Type]); if (request.Headers.ContainsKey("getcontentFeatures.dlna.org")) { if (item.MediaType == DlnaMediaTypes.Image) { headers.Add("contentFeatures.dlna.org", String.Format("{0};DLNA.ORG_OP=00;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={1}", item.PN, DlnaMaps.DefaultInteractive)); } else { headers.Add("contentFeatures.dlna.org", String.Format("{0};DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={1}", item.PN, DlnaMaps.DefaultStreaming)); } } Headers.Add("transferMode.dlna.org", transferMode); Debug(headers); }
public ItemResponse(string prefix, IRequest request, IMediaResource item, string transferMode = "Streaming") { this.item = item; headers = new ResponseHeaders(noCache: !(item is IMediaCoverResource)); var meta = item as IMetaInfo; if (meta != null) { headers.Add("Content-Length", meta.InfoSize.ToString()); headers.Add("Last-Modified", meta.InfoDate.ToString("R")); } headers.Add("Accept-Ranges", "bytes"); headers.Add("Content-Type", DlnaMaps.Mime[item.Type]); if (request.Headers.ContainsKey("getcontentFeatures.dlna.org")) { try { if (item.MediaType == DlnaMediaTypes.Image) { headers.Add( "contentFeatures.dlna.org", String.Format( "DLNA.ORG_PN={0};DLNA.ORG_OP=00;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={1}", item.PN, DlnaMaps.DefaultInteractive ) ); } else { headers.Add( "contentFeatures.dlna.org", String.Format( "DLNA.ORG_PN={0};DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={1}", item.PN, DlnaMaps.DefaultStreaming ) ); } } catch (NotSupportedException) { } catch (NotImplementedException) { } } if (request.Headers.ContainsKey("getCaptionInfo.sec")) { var mvi = item as IMetaVideoItem; if (mvi != null && mvi.Subtitle.HasSubtitle) { var surl = String.Format( "http://{0}:{1}{2}subtitle/{3}/st.srt", request.LocalEndPoint.Address, request.LocalEndPoint.Port, prefix, item.Id ); DebugFormat("Sending subtitles {0}", surl); headers.Add("CaptionInfo.sec", surl); } } if (request.Headers.ContainsKey("getMediaInfo.sec")) { var md = item as IMetaDuration; if (md != null && md.MetaDuration.HasValue) { headers.Add( "MediaInfo.sec", string.Format( "SEC_Duration={0};", md.MetaDuration.Value.TotalMilliseconds ) ); } } headers.Add("transferMode.dlna.org", transferMode); Debug(headers); }
internal Redirect(HttpCodes code, string uri) : base(code, "text/plain", "Redirecting...") { Headers.Add("Location", uri); }
private void ReadCallback(IAsyncResult result) { if (state == HttpStates.Closed) { return; } State = HttpStates.Reading; try { var read = stream.EndRead(result); if (read < 0) { throw new HttpException("Client did not send anything"); } DebugFormat("{0} - Read {1} bytes", this, read); readStream.Write(buffer, 0, read); lastActivity = DateTime.Now; } catch (Exception) { if (!IsATimeout) { WarnFormat("{0} - Failed to read data", this); Close(); } return; } try { if (!hasHeaders) { readStream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(readStream); for (var line = reader.ReadLine(); line != null; line = reader.ReadLine()) { line = line.Trim(); if (string.IsNullOrEmpty(line)) { hasHeaders = true; readStream = StreamManager.GetStream(); if (Headers.ContainsKey("content-length") && uint.TryParse(Headers["content-length"], out bodyBytes)) { if (bodyBytes > 1 << 20) { throw new IOException("Body too long"); } var ascii = Encoding.ASCII.GetBytes(reader.ReadToEnd()); readStream.Write(ascii, 0, ascii.Length); DebugFormat("Must read body bytes {0}", bodyBytes); } break; } if (Method == null) { var parts = line.Split(new[] { ' ' }, 3); Method = parts[0].Trim().ToUpperInvariant(); Path = parts[1].Trim(); DebugFormat("{0} - {1} request for {2}", this, Method, Path); } else { var parts = line.Split(new[] { ':' }, 2); Headers[parts[0]] = Uri.UnescapeDataString(parts[1]).Trim(); } } } if (bodyBytes != 0 && bodyBytes > readStream.Length) { DebugFormat( "{0} - Bytes to go {1}", this, bodyBytes - readStream.Length); Read(); return; } using (readStream) { Body = Encoding.UTF8.GetString(readStream.ToArray()); Debug(Body); Debug(Headers); } SetupResponse(); } catch (Exception ex) { Warn($"{this} - Failed to process request", ex); response = error500.HandleRequest(this); SendResponse(); } }