public static RtmptRequest DecodeBuffer(RtmpConnection connection, ByteBuffer stream) {
			RtmpContext context = connection.Context;
			int position = (int)stream.Position;
			try {
				BufferStreamReader sr = new BufferStreamReader(stream);
				string request = sr.ReadLine();
				string[] tokens = request.Split(new char[] { ' ' });
				string method = tokens[0];
				string url = tokens[1];
				// Decode all encoded parts of the URL using the built in URI processing class
				int i = 0;
				while ((i = url.IndexOf("%", i)) != -1) {
					url = url.Substring(0, i) + Uri.HexUnescape(url, ref i) + url.Substring(i);
				}
				// Lets just make sure we are using HTTP, thats about all I care about
				string protocol = tokens[2];// "HTTP/"
				//Read headers
				Hashtable headers = new Hashtable();
				string line;
				string name = null;
				while ((line = sr.ReadLine()) != null && line != string.Empty) {
					// If the value begins with a space or a hard tab then this
					// is an extension of the value of the previous header and
					// should be appended
					if (name != null && Char.IsWhiteSpace(line[0])) {
						headers[name] += line;
						continue;
					}
					// Headers consist of [NAME]: [VALUE] + possible extension lines
					int firstColon = line.IndexOf(":");
					if (firstColon != -1) {
						name = line.Substring(0, firstColon);
						string value = line.Substring(firstColon + 1).Trim();
						headers[name] = value;
					} else {
						//400, "Bad header: " + line
						break;
					}
				}
				RtmptRequest rtmptRequest = new RtmptRequest(connection, url, protocol, method, headers);
				if (stream.Remaining == rtmptRequest.ContentLength) {
					stream.Compact();
					rtmptRequest.Data = ByteBuffer.Wrap(stream.ToArray());
					stream.Flip();
					return rtmptRequest;
				} else {
					// Move the position back to the start
					stream.Position = position;
				}
			} catch {
				// Move the position back to the start
				stream.Position = position;
				throw;
			}
			return null;
		}
Exemple #2
0
		private void ReturnMessage(RtmptConnection connection, ByteBuffer data, HttpResponse response) {
			response.StatusCode = 200;
			response.ClearHeaders();
			response.AppendHeader("Connection", "Keep-Alive");
			int contentLength = data.Limit + 1;
			response.AppendHeader("Content-Length", contentLength.ToString());
			response.Cache.SetCacheability(HttpCacheability.NoCache);
			response.ContentType = ContentType.RTMPT;
			response.Write((char)connection.PollingDelay);
			byte[] buffer = data.ToArray();
			response.OutputStream.Write(buffer, 0, buffer.Length);
			response.Flush();
		}
Exemple #3
0
		private void ReturnMessage(RtmptConnection connection, ByteBuffer data, RtmptRequest request) {
			ByteBuffer buffer = ByteBuffer.Allocate((int)data.Length + 30);
			StreamWriter sw = new StreamWriter(buffer);
			int contentLength = data.Limit + 1;
			if (request.HttpVersion == 1) {
				sw.Write("HTTP/1.1 200 OK\r\n");
				sw.Write("Cache-Control: no-cache\r\n");
			} else {
				sw.Write("HTTP/1.0 200 OK\r\n");
				sw.Write("Pragma: no-cache\r\n");
			}
			sw.Write(string.Format("Content-Length: {0}\r\n", contentLength));
			sw.Write("Connection: Keep-Alive\r\n");
			sw.Write(string.Format("Content-Type: {0}\r\n", ContentType.RTMPT));
			sw.Write("\r\n");
			sw.Write((char)connection.PollingDelay);
			sw.Flush();
			BinaryWriter bw = new BinaryWriter(buffer);
			byte[] buf = data.ToArray();
			bw.Write(buf);
			bw.Flush();
			request.Connection.Write(buffer);
		}
		public override void Write(ByteBuffer buffer) {
			byte[] buf = buffer.ToArray();
			Write(buf);
		}