internal static Torrent CreateMultiFileTorrent() { Hashtable metainfo = new Hashtable(); Hashtable info = new Hashtable(); ArrayList files = new ArrayList(); Hashtable file1 = new Hashtable(); Hashtable file2 = new Hashtable(); ArrayList file1Path = new ArrayList( new ByteString[] { new ByteString("dir1"), new ByteString("file1") }); ArrayList file2Path = new ArrayList( new ByteString[] { new ByteString("dir1"), new ByteString("dir2"), new ByteString("file2") }); metainfo["announce"] = new ByteString("http://localhost:82/announce"); metainfo["info"] = info; info["name"] = new ByteString("test"); info["piece length"] = 512 * 1024L; info["pieces"] = new ByteString(new byte[40]); info["files"] = files; files.Add(file1); files.Add(file2); file1["length"] = 512 * 1024L; file1["path"] = file1Path; file2["length"] = 256 * 1024L; file2["path"] = file2Path; return new Torrent(metainfo); }
public void ParseOneField() { ByteString responseText = new ByteString("HTTP/1.1 200 OK\r\n" + "Header1: Value1\r\n\r\n"); HttpResponse response = new HttpResponse(responseText); Assert.AreEqual("Value1", response.Fields["Header1"]); }
public void ParseContent() { ByteString responseText = new ByteString("HTTP/1.1 200 OK\r\n" + "Header1: Value1\r\n" + "Header2: Value2\r\n\r\n" + "content"); HttpResponse response = new HttpResponse(responseText); Assert.AreEqual(new ByteString("content"), response.Content); }
public static ByteString CreateTestResponseString() { Hashtable response = new Hashtable(); response["interval"] = 10; response["tracker id"] = "some tracker id"; response["complete"] = 1; response["incomplete"] = 2; response["peers"] = new ByteString(new byte[] { 127, 0, 0, 1, 0x1a, 0xe1, 207, 142, 131, 248, 0x1a, 0xe2, 105, 100, 107, 120, 0x1a, 0xe3 }); ByteString encodedResponseBody = new ByteString(BEncoder.Encode(response)); return new ByteString(string.Format("HTTP/1.1 OK\r\n\r\n{0}", encodedResponseBody.ToString())); }
internal static Torrent CreateSingleFileTorrent() { Hashtable metainfo = new Hashtable(); Hashtable info = new Hashtable(); metainfo["announce"] = new ByteString("http://localhost:82"); info["name"] = new ByteString("test"); info["length"] = 512 * 1024L + 256 * 1024L; info["piece length"] = 512 * 1024L; info["pieces"] = new ByteString(new byte[40]); metainfo["info"] = info; return new Torrent(metainfo); }
public void SendStartedMessage() { tracker = new Tracker(file, socket); bool connected = false; tracker.Connected += delegate(object sender, EventArgs e) { connected = true; }; ByteString expectedMessage = new ByteString("GET /announce?info_hash=%cdt%feF%cc%a3%a0%e6%dc_%f7%1c%bd%82%82ljZ%5d%3e&" + "peer_id=12345678901234567890&port=6881&compact=1&uploaded=0&downloaded=0&left=3706908089&event=started HTTP/1.1\r\n\r\n"); tracker.Start(); Assert.IsTrue(socket.Connected); Assert.IsTrue(connected); Assert.AreEqual(expectedMessage, socket.lastMessage); }
public TrackerResponse(ByteString responseText) { HttpResponse response = new HttpResponse(responseText); BenDecoder decoder = new BenDecoder(new MemoryStream(response.Content.ToBytes())); responseContent = decoder.ReadDictionary(); if(IsSuccessful) { ByteString peers = responseContent["peers"] as ByteString; BinaryReader reader = new BinaryReader(new MemoryStream(peers.ToBytes())); for(int i = 0; i<peers.ToBytes().Length; i+=6) { peerList.Add(new PeerInfo(reader.ReadBytes(4), reader.ReadInt16())); } } }
public HttpResponse(ByteString responseText) { int position = responseText.IndexOf("\r\n"); statusLine = responseText.SubString(0, position).ToString(); position += 2; int lineEnd = responseText.IndexOf("\r\n", position); ByteString fieldLine = responseText.SubString(position, lineEnd - position); while(!fieldLine.ToString().Equals("")) { fields[fieldLine.Split(':')[0].Trim().ToString()] = fieldLine.Split(':')[1].Trim().ToString(); position = lineEnd + 2; lineEnd = responseText.IndexOf("\r\n", position); fieldLine = responseText.SubString(position, lineEnd - position); } content = responseText.SubString(lineEnd + 2); }
public PeerId(ByteString id):this(id.ToString()) { }
public void ParseStatusLine() { ByteString responseText = new ByteString("HTTP/1.1 200 OK\r\n\r\n"); HttpResponse response = new HttpResponse(responseText); Assert.AreEqual("HTTP/1.1 200 OK", response.StausLine); }