int ReadReqHeaderReqRow(HttpHeader header, char[] pack, int packlen, ref int startpos, ref int identlen) { int nextpos; header.type = 0; //请求方法 nextpos = GetIdentifier(' ', pack, packlen, ref startpos, ref identlen); if (identlen == 0) { return(0); } string key = new string(pack, startpos, identlen); header.method = ShardRes.GetInstance().GetHttpRequestMethod(key); if (header.method == HttpRequestMethod.HTTP_METHOD_UNKNOWN) { return(0); } //URL startpos = nextpos; nextpos = GetIdentifier(' ', pack, packlen, ref startpos, ref identlen); string a = new string(pack, startpos, 1); int cmp = string.Compare(a, "/"); if (identlen == 0 || cmp != 0) { return(0); } header.url = new string(pack, startpos, identlen); //协议版本 startpos = nextpos; nextpos = GetIdentifier('\r', pack, packlen, ref startpos, ref identlen, true); if (identlen == 0) { return(0); } header.ver = new string(pack, startpos, identlen); //\r\n startpos = nextpos; nextpos = MatchIdentifier("\r\n", pack, packlen, ref startpos, ref identlen); if (identlen == 0) { return(0); } return(nextpos); }
string CreateHttpReqHeader(HttpHeader header) { //请求头 if (header.type == 0) { string method = ShardRes.GetInstance().GetHttpRequestMethodStr(header.method); string req = method + " " + header.url + " " + header.ver + "\r\n"; req += CreateHttpHeaderOptionStr(header) + "\r\n"; return(req); } //响应头 string first = header.ver + " " + header.stateCode + " " + header.stateMsg + "\r\n"; string headOpts = CreateHttpHeaderOptionStr(header); first += headOpts + "\r\n"; return(first); }
/// <summary> /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// </summary> /// <returns></returns> public static ShardRes GetInstance() { // 当第一个线程运行到这里时,此时会对locker对象 "加锁", // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁 // lock语句运行完之后(即线程运行完之后)会对该对象"解锁" // 双重锁定只需要一句判断就可以了 if (uniqueInstance == null) { lock (locker) { // 如果类的实例不存在则创建,否则直接返回 if (uniqueInstance == null) { uniqueInstance = new ShardRes(); } } } return(uniqueInstance); }
public override int ReadStreamToHeader(HttpHeader header, ByteStream readStream, bool isNetToHost) { readStream.isByteAlign = false; byte[] bytepack = readStream.ReadBytes(readStream.GetBufSize()); char[] pack = Encoding.ASCII.GetChars(bytepack); int packlen = readStream.GetBufSize(); int state = 0; int startpos = 0, identlen = 0, nextpos = 0; while (true) { switch (state) { case 0: { GetIdentifier(' ', pack, packlen, ref startpos, ref identlen); if (identlen == 0) { goto error; } string key = new string(pack, startpos, identlen); header.method = ShardRes.GetInstance().GetHttpRequestMethod(key); if (header.method != HttpRequestMethod.HTTP_METHOD_UNKNOWN) { startpos = ReadReqHeaderReqRow(header, pack, packlen, ref startpos, ref identlen); } else { startpos = ReadRespHeaderStateRow(header, pack, packlen, ref startpos, ref identlen); } if (startpos == 0) { goto error; } state = 1; } break; case 1: { while (true) { //头部字段名 nextpos = GetIdentifier(':', pack, packlen, ref startpos, ref identlen, true); if (identlen == 0) { startpos = nextpos; nextpos = MatchIdentifier("\r\n", pack, packlen, ref startpos, ref identlen); if (identlen == 0) { goto error; } goto end; } string field = new string(pack, startpos, identlen); //头部字段值 startpos = nextpos + 1; nextpos = GetIdentifier('\r', pack, packlen, ref startpos, ref identlen, true); if (identlen != 0) { string value = new string(pack, startpos, identlen); header.fieldMap[field] = value; } startpos = nextpos; nextpos = MatchIdentifier("\r\n", pack, packlen, ref startpos, ref identlen); if (identlen == 0) { goto error; } startpos = nextpos; } } // break; } } error: header.isVaild = false; return(0); end: header.isVaild = true; return(readStream.GetNumberOfCurtBytes()); }