AppendHeader() публичный Метод

public AppendHeader ( string name, string value ) : void
name string
value string
Результат void
Пример #1
0
        public override void Process(HttpListenerRequest request, HttpListenerResponse response)
        {
            string[] actionPath = request.RawUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            string action = actionPath[1].ToLower();

            ISongPlayer songPlayer = SongPlayerFactory.GetSongPlayer();
            string requester = GetRequester(request);

            response.AppendHeader("Cache-Control", "no-cache");

            switch (action)
            {
                case "queue":
                    switch (request.HttpMethod)
                    {
                        case "GET":
                            response.ContentType = "application/json";
                            WriteUtf8String(response.OutputStream, JsonConvert.SerializeObject(
                                new
                                {
                                    Queue = songPlayer.PlayQueue.ToList(),
                                    PlayerStatus = songPlayer.PlayerStatus,
                                    Self = requester
                                }
                            ));
                            break;
                        case "POST":
                            if (!ClientAllowed(requester))
                                return;

                            using (var reader = new StreamReader(request.InputStream))
                            {
                                songPlayer.Enqueue(reader.ReadToEnd(), requester);
                            }
                            break;
                        case "DELETE":
                            if (!ClientAllowed(requester))
                                return;

                            using (var reader = new StreamReader(request.InputStream))
                            {
                                songPlayer.Dequeue(reader.ReadToEnd(), requester);
                            }
                            break;
                    }
                    break;
                case "playlist":
                    {
                        if (!ClientAllowed(requester))
                            return;

                        if (request.HttpMethod == "POST")
                        {
                            using (var reader = new StreamReader(request.InputStream))
                            {
                                string posted = reader.ReadToEnd();
                                var playlistRequest = JsonConvert.DeserializeAnonymousType(posted, new { Filter = string.Empty, Page = 0, SortBy = "artist", Ascending = true });

                                Song[] songs = songPlayer.GetPlayList(
                                    playlistRequest.Filter,
                                    playlistRequest.SortBy,
                                    playlistRequest.Ascending
                                ).ToArray();

                                response.ContentType = "application/json";
                                WriteUtf8String(response.OutputStream, JsonConvert.SerializeObject(
                                    new
                                    {
                                        TotalPageCount = (songs.Length + (_pageSize - 1)) / _pageSize,
                                        CurrentPage = playlistRequest.Page,
                                        SongsForCurrentPage = songs.Skip((playlistRequest.Page - 1) * _pageSize).Take(_pageSize).ToArray(),
                                        SortBy = playlistRequest.SortBy,
                                        Ascending = playlistRequest.Ascending
                                    }
                                ));
                            }
                        }
                        break;
                    }
                case "next":
                    if (!ClientAllowed(requester))
                        return;

                    response.ContentType = "application/json";
                    songPlayer.Next(requester);
                    break;
                case "rescan":
                    if (!ClientAllowed(requester))
                        return;

                    response.ContentType = "application/json";
                    songPlayer.Rescan();
                    ImageHelper.Purge();
                    break;
                case "pause":
                    if (!ClientAllowed(requester))
                        return;

                    response.ContentType = "application/json";
                    songPlayer.Pause();
                    break;
                case "volume":
                    if (!ClientAllowed(requester))
                        return;

                    response.ContentType = "application/json";
                    if (request.HttpMethod == "POST")
                    {
                        using (var reader = new StreamReader(request.InputStream))
                        {
                            string posted = reader.ReadToEnd();

                            int volume;
                            if (int.TryParse(posted, out volume))
                            {
                                songPlayer.Volume = volume;
                            }

                            WriteUtf8String(response.OutputStream, JsonConvert.SerializeObject(songPlayer.Volume));
                        }
                    }
                    break;
                case "image":
                    string tempId;
                    bool large = false;
                    if (actionPath.Length == 4)
                    {
                        tempId = actionPath[2];
                        if (!string.IsNullOrEmpty(tempId))
                        {
                            tempId = WebUtility.HtmlDecode(tempId);
                        }

                        if (actionPath[3].Equals("large", StringComparison.OrdinalIgnoreCase))
                        {
                            large = true;
                        }
                    }
                    else
                    {
                        tempId = string.Empty;

                        if (actionPath[2].Equals("large", StringComparison.OrdinalIgnoreCase))
                        {
                            large = true;
                        }
                    }

                    ImageHelper.HelpMe(response, tempId, songPlayer, large);
                    break;
                default:
                    response.ContentType = "text/plain";
                    WriteUtf8String(response.OutputStream, request.RawUrl);
                    break;
            }
        }
Пример #2
0
		static bool ServeMvcResponse(HttpListenerResponse httpResponse, HttpResponse mvcResponse)
		{
			var status = mvcResponse.Status;
			httpResponse.StatusCode = status;
			httpResponse.ContentType = mvcResponse.ContentType;
			httpResponse.RedirectLocation = mvcResponse.RedirectUrl;

			// headers
			if (mvcResponse.Headers.Count > 0)
			{
				foreach (var header in mvcResponse.Headers)
				{
					httpResponse.AppendHeader(header.Key as String, header.Value as String);
				}
			}

			// cookies
			var cookies = mvcResponse.Cookies;
			if(cookies != null && cookies.Count > 0)
			{
				foreach (var cookie in cookies.Values)
				{
					var cookieHeader = GetHeaderValue(cookie);
					httpResponse.Headers.Add("Set-Cookie", cookieHeader);
				}
			}

			// body
			var body = mvcResponse.Body;
			if (body != null)
			{
				byte[] buffer;

				var byteArrayBody = body as byte[];                
				if (byteArrayBody != null)
				{
					buffer = byteArrayBody;
				}
				else
				{
					// si no son bytes tratarlo siempre como un string.
					buffer = Encoding.UTF8.GetBytes(body.ToString());
				}

				httpResponse.ContentLength64 = buffer.LongLength;
				httpResponse.OutputStream.Write(buffer, 0, buffer.Length);
			}

			return true;
		}