} // DO YOU have to account for directories public string GetPath(File_Jrod file) { StringBuilder n = new StringBuilder(); List <string> h = new List <string>(); h.Add(file.Name); Dir_Jrod temp = file.Parent; while (temp != null) { h.Add(temp.Name); temp = temp.Parent; } h.Reverse(); n.Append("/"); for (int i = 0; i < h.Count; i++) { n.Append(h[i]); if (i == h.Count - 1) { break; } n.Append("/"); } return(n.ToString()); }
public virtual bool Contains(File_Jrod file) { return(Contains(file.Parent)); }
// respond with the correct file public void RespondWithFile(File_Jrod file, WebRequest req) { Stream filecontents = file.OpenReadOnly(); long end = filecontents.Length; byte[] buffer = new byte[1024 * 6]; int bytes_read = 0; // total amount of bytes read. int thisread = 0; // for each read we preform StringBuilder j = new StringBuilder(); string encodings; if (_mappings.ContainsKey(Path.GetExtension(req.URI))) { encodings = _mappings[Path.GetExtension(req.URI)]; // Stream file_length = File.Open(file.Name, FileMode.Open, FileAccess.Read); } else { encodings = "text/plain"; } /* if we have are supposed to break the data into chunks, we might as well search/read until the * correct byte is reach.*/ // account for range. we need to know when what weve read in has reached the point to startsend to client if (req.headers.ContainsKey("Content-Range")) { // split the string so we can investigate whether we have a x-y or x- or -y. Three cases. string[] range_ = req.headers["Content-Range"].Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries); // how many numbers do we have to work with? if (range_.Length == 1) { // is the dash in front of our one number or behind it? // in this case befor if ((req.headers["Content-Range"])[0] == '-') { // set this as the end value end = Convert.ToInt32(range_[0]); } //if were looking for the start number and the dash is at the ende else if ((req.headers["Content-Range"])[(req.headers["Content-Range"]).Length - 1] == '-') { // might need protection against it being longer int readtothis = Convert.ToInt32(range_[0]); int next_read = 0; next_read = readtothis - bytes_read; while (bytes_read < readtothis) { next_read = readtothis - bytes_read; // determine how much to read into the buffer if (readtothis - bytes_read > 1024 * 6) { next_read = 1024 * 6;// set it to the max buffer size } // dont need an offset we aren't saving these bytes bytes_read += filecontents.Read(buffer, 0, next_read); } } else { return; } }// if we have two numbers enter this else if (range_.Length == 2) { // might need protection against it being longer int readtothis = Convert.ToInt32(range_[0]); int next_read = 0; next_read = readtothis - bytes_read; while (bytes_read < readtothis) { next_read = readtothis - bytes_read; // determine how much to read into the buffer if (readtothis - bytes_read > 1024 * 6) { next_read = 1024 * 6;// set it to the max buffer size } // dont need an offset we aren't saving these bytes bytes_read += filecontents.Read(buffer, 0, next_read); } end = Convert.ToInt16(range_[1]); } //too many dashes or invalid iinput break! else { return; } }// else if we dont have the range header j.Append(String.Format("{0} 200 OK\r\nContent-Type:{1}\r\n" + "Content-Length:{2}\r\n\r\n" , req.version, encodings, Convert.ToInt32(end) - bytes_read)); byte[] sendthis = Encoding.ASCII.GetBytes(j.ToString()); req.Response.Write(sendthis, 0, sendthis.Length); while (bytes_read < end) { int next_read = Convert.ToInt32(end) - bytes_read; // determine how much to read into the buffer if (Convert.ToInt32(end) - bytes_read > 1024 * 6) { next_read = 1024 * 6;// set it to the max buffer size } thisread = filecontents.Read(buffer, 0, next_read); bytes_read += thisread; req.Response.Write(buffer, 0, thisread); } }
public override void Handler(WebRequest req) { if (!req.URI.StartsWith(this.ServiceURI)) { throw new InvalidOperationException(); } //percent-decode URI string [] first_split = req.URI.Split(new [] { "/" }, StringSplitOptions.RemoveEmptyEntries); string[] pieces = new string[first_split.Length - 1]; Dir_Jrod dir = r_sys.GetRoot(); // check the make sure there wasnt garbage after files if (first_split[0] != "files") { req.WriteNotFoundResponse(req.URI); return; } if (first_split.Length == 1) { req.WriteHTMLResponse(BuildDirHTML(dir)); return; } // all this loop is doing is removing the first iteam in pieces the / file extentsion for (int i = 1; i < first_split.Length; i++) { pieces[i - 1] = first_split[i]; } // we skip to one, forget the "files" for now //edit I plan on for (int i = 1; i < pieces.Length - 1; i++) { string piece = pieces[i]; dir = dir.GetDir(piece); if (dir == null) { req.WriteNotFoundResponse(req.URI); return; } } //one piece left to process File_Jrod file = dir.GetFile(pieces[pieces.Length - 1]); if (file != null) { RespondWithFile(file, req); return; } else { dir = dir.GetDir(pieces[pieces.Length - 1]); if (dir != null) { req.WriteHTMLResponse(BuildDirHTML(dir)); return; } else { req.WriteNotFoundResponse(req.URI); return; } } }