/// <summary> /// If validation with the DEA is successful, the HTTP request is served. /// Otherwise, the same HTTP response from the DEA is served as response to /// the HTTP request. /// </summary> /// <param name="listenerContext">Http context to respond to.</param> private void ServeHttp(object listenerContext) { try { HttpListenerContext context = (HttpListenerContext)listenerContext; Uri uri = context.Request.Url; PathLookupResponse response = this.deaClient.LookupPath(uri); if (!string.IsNullOrWhiteSpace(response.Error)) { Logger.Warning(Strings.ErrorInLookupPath, response.Error); DirectoryServer.WriteServerError(Strings.WinDEADidNotRespondProperly, context); } else { var queryString = string.Join(string.Empty, uri.PathAndQuery.Split('?').Skip(1)); NameValueCollection queryStrings = System.Web.HttpUtility.ParseQueryString(queryString); bool tail = queryStrings.AllKeys.Select(key => queryStrings.GetValues(key).Contains("tail")).Any(v => v); Logger.Debug("Directory Server file request is: {0}; path is {1}", uri, queryStrings["path"]); this.ListPath(response.Path, tail, context); } } catch (Exception ex) { Logger.Error("Directory Server - there was an error serving http: {0}", ex.ToString()); } }
private void WriteFile(string path, bool tail, HttpListenerContext context) { try { if (tail) { Logger.Debug("Directory server tailing file '{0}'", path); string mimeType = DetectContentType(path); context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK; context.Response.ContentType = mimeType; StreamHandler streamHandler = new StreamHandler(path, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(this.streamingTimeout), context); streamHandler.Start(); } else { DumpFile(path, context); } } catch (Exception ex) { DirectoryServer.WriteServerError(ex.Message, context); } }
/// <summary> /// Lists directory, or writes file contents in the HTTP response as per the /// the response received from the DEA. If the "tail" parameter is part of /// the HTTP request, then the file contents are streamed through chunked /// HTTP transfer encoding. Otherwise, the entire file is dumped in the HTTP /// response. /// Writes appropriate errors and status codes in the HTTP response if there is /// a problem in reading the file or directory. /// </summary> /// <param name="path">The path.</param> /// <param name="tail">if set to <c>true</c> it means we tail the file live.</param> /// <param name="context">Http context to respond to.</param> private void ListPath(string path, bool tail, HttpListenerContext context) { if (File.Exists(path)) { this.WriteFile(path, tail, context); } else if (Directory.Exists(path)) { DirectoryServer.ListDir(path, context); } else { Logger.Warning(Strings.PathNotFound, path); DirectoryServer.WriteEntityNotFound(context); } }