object ProcessManageFile(IHttpRequest req, HttpResponseHeader res) { string str_key = req.Url.AbsolutePath.Substring (8); Key key; try { if (str_key.Length == (DefaultAlgorithm.ECDomainBytes + 1) * 2) key = Key.Parse (str_key); else if (str_key.Length == (DefaultAlgorithm.ECDomainBytes + 1) * 4 / 3) key = Key.FromUriSafeBase64String (str_key); else throw new HttpException (HttpStatusCode.NotFound); } catch { throw new HttpException (HttpStatusCode.NotFound); } MergeableFileHeader header; IMergeableFileWebUIHelper header_helper = null; if (req.HttpMethod == HttpMethod.POST && req.HasContentBody ()) { header = _node.MMLC.GetMergeableFileHeader (key); if (header == null) throw new HttpException (HttpStatusCode.NotFound); header_helper = (header.Content as IMergeableFile).WebUIHelper; NameValueCollection c = HttpUtility.ParseUrlEncodedStringToNameValueCollection (Encoding.ASCII.GetString (req.GetContentBody (MaxRequestBodySize)), Encoding.UTF8); AuthServerInfo[] auth_servers = AuthServerInfo.ParseArray (c["auth"]); List<Key> list = new List<Key> (); string[] keep_array = c.GetValues ("record"); if (keep_array != null) { for (int i = 0; i < keep_array.Length; i++) { list.Add (Key.FromUriSafeBase64String (keep_array[i])); } } IHashComputable new_header_content = header_helper.CreateHeaderContent (c); string title = c["title"]; if (title == null || title.Length == 0 || title.Length > 64) throw new HttpException (HttpStatusCode.InternalServerError); MergeableFileHeader new_header = new MergeableFileHeader (key, title, header.Flags, header.CreatedTime, new_header_content, auth_servers); _node.MMLC.Manage (new_header, list.ToArray (), null); res[HttpHeaderNames.Location] = header_helper.ViewUrl + key.ToUriSafeBase64String (); throw new HttpException (req.HttpVersion == HttpVersion.Http10 ? HttpStatusCode.Found : HttpStatusCode.SeeOther); } List<MergeableFileRecord> records = _node.MMLC.GetRecords (key, out header); if (header == null || records == null) throw new HttpException (HttpStatusCode.NotFound); header_helper = (header.Content as IMergeableFile).WebUIHelper; XmlDocument doc = XmlHelper.CreateEmptyDocument (); doc.DocumentElement.AppendChild (XmlHelper.CreateMergeableFileElement (doc, header, records.ToArray ())); return _xslTemplate.Render (req, res, doc, Path.Combine (DefaultTemplatePath, header_helper.ManagePageXslFileName)); }
object ProcessNetInitPage(IHttpRequest req, HttpResponseHeader res) { XmlDocument doc = XmlHelper.CreateEmptyDocument (); if (req.HttpMethod == HttpMethod.POST && req.HasContentBody ()) { Dictionary<string, string> dic = HttpUtility.ParseUrlEncodedStringToDictionary (Encoding.ASCII.GetString (req.GetContentBody (MaxRequestBodySize)), Encoding.UTF8); if (dic.ContainsKey ("nodes")) { string[] lines = dic["nodes"].Replace ("\r\n", "\n").Replace ('\r', '\n').Split ('\n'); List<EndPoint> list = new List<EndPoint> (); List<string> raw_list = new List<string> (); for (int i = 0; i < lines.Length; i++) { EndPoint ep; lines[i] = lines[i].Trim (); if (lines[i].StartsWith ("%")) { ep = EndPointObfuscator.Decode (lines[i]); } else { ep = Helpers.Parse (lines[i]); } if (ep != null) { list.Add (ep); raw_list.Add (lines[i]); } } if (list.Count > 0) { p2pncs.Threading.ThreadTracer.CreateThread (delegate () { for (int i = 0; i < list.Count; i++) { if (list[i] is IPEndPoint) { if ((list[i] as IPEndPoint).Address.Equals (_node.GetCurrentPublicIPAddress ())) continue; _node.PortOpenChecker.Join (list[i]); } } for (int i = 0; i < list.Count; i++) { if (list[i] is DnsEndPoint) { IPAddress[] adrs_list = Dns.GetHostAddresses ((list[i] as DnsEndPoint).DNS); for (int k = 0; k < adrs_list.Length; k++) { if (adrs_list[k].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !adrs_list[k].Equals (_node.GetCurrentPublicIPAddress ())) { _node.PortOpenChecker.Join (new IPEndPoint (adrs_list[k], (list[i] as DnsEndPoint).Port)); } } } } }, "WebApp Join Thread").Start (); XmlNode root = doc.DocumentElement.AppendChild (doc.CreateElement ("connected")); for (int i = 0; i < list.Count; i++) { XmlElement element = doc.CreateElement ("endpoint"); element.AppendChild (doc.CreateTextNode (raw_list[i].ToString ())); root.AppendChild (element); } } } else if (dic.ContainsKey ("ip") && dic.ContainsKey ("port")) { string ip_dns = dic["ip"].Trim (); string port = dic["port"].Trim (); try { if (ip_dns.Length == 0) throw new FormatException (); EndPoint ep = Helpers.Parse (ip_dns + ":" + port); if (ep == null) throw new FormatException (); if (ep is IPEndPoint && (IPAddressUtility.IsPrivate ((ep as IPEndPoint).Address) || (ep as IPEndPoint).Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)) throw new FormatException (); string encoded = EndPointObfuscator.Encode (ep); doc.DocumentElement.AppendChild (doc.CreateElement ("encoded", null, new XmlNode[] { doc.CreateElement ("source", null, new[] { doc.CreateTextNode (ip_dns + ":" + port) }), doc.CreateTextNode (encoded) })); } catch { doc.DocumentElement.AppendChild (doc.CreateElement ("encoded", null, new[] { doc.CreateElement ("source", null, new[] { doc.CreateTextNode (ip_dns + ":" + port) }), doc.CreateElement ("error") })); } } } string pub_ip = ""; XmlNode[] encoded_nodes = null; if (!IPAddressUtility.IsPrivate (_node.GetCurrentPublicIPAddress ())) { pub_ip = _node.GetCurrentPublicIPAddress().ToString (); encoded_nodes = new XmlNode[] { doc.CreateTextNode (EndPointObfuscator.Encode (new IPEndPoint (_node.GetCurrentPublicIPAddress (), _node.BindUdpPort))) }; } doc.DocumentElement.AppendChild (doc.CreateElement ("ipendpoint", new string[][] { new [] {"ip", pub_ip}, new [] {"port", _node.BindUdpPort.ToString ()} }, encoded_nodes)); return _xslTemplate.Render (req, res, doc, Path.Combine (DefaultTemplatePath, "net_init.xsl")); }
object ProcessFileOpen(IHttpRequest req, HttpResponseHeader res) { if (req.HttpMethod == HttpMethod.POST && req.HasContentBody ()) { Dictionary<string, string> dic = HttpUtility.ParseUrlEncodedStringToDictionary (Encoding.ASCII.GetString (req.GetContentBody (MaxRequestBodySize)), Encoding.UTF8); string key; if (dic.TryGetValue ("id", out key)) { res[HttpHeaderNames.Location] = key; throw new HttpException (req.HttpVersion == HttpVersion.Http10 ? HttpStatusCode.Found : HttpStatusCode.SeeOther); } } XmlDocument doc = XmlHelper.CreateEmptyDocument (); return _xslTemplate.Render (req, res, doc, Path.Combine (DefaultTemplatePath, "open.xsl")); }
object ProcessWikiPage(Node node, IHttpRequest req, HttpResponseHeader res, MergeableFileHeader header, List<MergeableFileRecord> records, string page_title) { XmlDocument doc = XmlHelper.CreateEmptyDocument (); MergeableFileRecord record = null; if (req.HttpMethod == HttpMethod.POST && req.HasContentBody ()) { Dictionary<string, string> dic = HttpUtility.ParseUrlEncodedStringToDictionary (Encoding.ASCII.GetString (req.GetContentBody (WebApp.MaxRequestBodySize)), Encoding.UTF8); record = new MergeableFileRecord (ParseNewPostData (dic), DateTime.UtcNow, header.LastManagedTime, null, null, null, 0, null); if (dic.ContainsKey ("preview")) { doc.DocumentElement.SetAttribute ("state", "preview"); } else { throw new HttpException (HttpStatusCode.Forbidden); } } string xsl = "wiki.xsl"; bool edit_mode = req.QueryData.ContainsKey ("edit"); bool history_mode = req.QueryData.ContainsKey ("history") & !edit_mode; if (record == null && !history_mode) record = GetLatestPage (records, page_title); if (!history_mode) { if (edit_mode) { xsl = "wiki_edit.xsl"; if (req.HttpMethod == HttpMethod.GET && record != null) (record.Content as WikiRecord).Name = ""; } if (record == null) doc.DocumentElement.AppendChild (XmlHelper.CreateMergeableFileElement (doc, header)); else doc.DocumentElement.AppendChild (XmlHelper.CreateMergeableFileElement (doc, header, new MergeableFileRecord[] { record })); } else if (history_mode) { records.RemoveAll (delegate (MergeableFileRecord ri) { WikiRecord wr = (WikiRecord)ri.Content; return !wr.PageName.Equals (page_title); }); doc.DocumentElement.AppendChild (XmlHelper.CreateMergeableFileElement (doc, header, records.ToArray ())); xsl = "wiki_history.xsl"; } doc.DocumentElement.AppendChild (doc.CreateElement ("page-title", null, new[] { doc.CreateTextNode (page_title) })); doc.DocumentElement.AppendChild (doc.CreateElement ("page-title-for-url", null, new[] { doc.CreateTextNode (WikiTitleToUrl (page_title)) })); return WebApp.Template.Render (req, res, doc, Path.Combine (WebApp.DefaultTemplatePath, xsl)); }