Exemplo n.º 1
0
        /// <summary>
        /// 从HTTP数据读取信息,然后让子类处理
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>状态</returns>
        public static int OnReceive(HttpData data, HttpFileData fileData)
        {
            FileStream fs = new FileStream(@"C:\Miao\a.zip", FileMode.Append);

            fs.Write(fileData.m_buffer, 0, fileData.m_buffer.Length);
            fs.Close();
            return(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 接受请求
        /// </summary>
        /// <param name="param">参数</param>
        private static void ReadData(object param)
        {
            Socket      socket      = (Socket)param;
            NodeService nodeService = DataCenter.NodeService;
            int         newSocketID = (int)socket.Handle;

            try
            {
                byte[]       buffer       = new byte[102400];
                int          len          = socket.Receive(buffer);
                MemoryStream memoryStream = new MemoryStream(buffer);
                StreamReader reader       = new StreamReader(memoryStream);
                HttpData     data         = new HttpData();
                data.m_remoteIP   = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
                data.m_remotePort = ((IPEndPoint)socket.RemoteEndPoint).Port;
                String requestHeader;
                int    contentLength = 0;
                string parameters    = "";
                while ((requestHeader = reader.ReadLine()) != null && !String.IsNullOrEmpty(requestHeader))
                {
                    String lowerHeader = requestHeader.ToLower();
                    if (lowerHeader.IndexOf("get") == 0)
                    {
                        int end = lowerHeader.IndexOf("http/");
                        data.m_method = "GET";
                        parameters    = requestHeader.Substring(5, end - 6);
                    }
                    else if (lowerHeader.IndexOf("post") == 0)
                    {
                        int end = lowerHeader.IndexOf("http/");
                        data.m_method = "POST";
                        parameters    = requestHeader.Substring(5, end - 6);
                    }
                    else if (lowerHeader.IndexOf("accept: ") == 0)
                    {
                        try
                        {
                            data.m_contentType = requestHeader.Substring(8, requestHeader.IndexOf(',') - 8);
                        }
                        catch { }
                    }
                    else if (lowerHeader.IndexOf("content-type:") == 0)
                    {
                        data.m_contentType = requestHeader.Substring(14);
                    }
                    else if (lowerHeader.IndexOf("host:") == 0)
                    {
                        data.m_url = requestHeader.Substring(requestHeader.IndexOf(':') + 2);
                    }
                    else if (lowerHeader.IndexOf("content-length") == 0)
                    {
                        int    begin = lowerHeader.IndexOf("content-length:") + "content-length:".Length;
                        String postParamterLength = requestHeader.Substring(begin).Trim();
                        contentLength = Convert.ToInt32(postParamterLength);
                    }
                }
                if (contentLength > 0)
                {
                    if (data.m_contentType != null && data.m_contentType == "application/octet-stream(test)")
                    {
                        HttpFileData fileHttpData = new HttpFileData();
                        int          recvLength   = 0;
                        while (recvLength < contentLength)
                        {
                            byte[] bytes = new byte[10240];
                            recvLength += socket.Receive(bytes);
                            fileHttpData.m_recvLength  = recvLength;
                            fileHttpData.m_totalLength = contentLength;
                            fileHttpData.m_buffer      = bytes;
                            FileTransferService.OnReceive(data, fileHttpData);
                        }
                    }
                    else
                    {
                        int idx = 0, ide = 0;
                        data.m_body = new byte[contentLength];
                        while (idx < contentLength)
                        {
                            int recvData = reader.Read();
                            if (recvData != -1)
                            {
                                if (recvData != 0)
                                {
                                    ide++;
                                }
                                idx++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        reader.Close();
                        memoryStream.Dispose();
                        if (ide == 0)
                        {
                            socket.Receive(data.m_body);
                        }
                        else
                        {
                            for (int i = 0; i < contentLength; i++)
                            {
                                data.m_body[i] = buffer[len - contentLength + i];
                            }
                        }
                    }
                    data.m_contentLength = contentLength;
                }
                else
                {
                    reader.Close();
                    memoryStream.Dispose();
                }
                if (data.m_method.Length == 0)
                {
                    return;
                }
                int cindex = parameters.IndexOf('?');
                if (cindex != -1)
                {
                    data.m_url = data.m_url + "/" + parameters;
                    parameters = parameters.Substring(cindex + 1);
                    String[] strs     = parameters.Split(new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
                    int      strsSize = strs.Length;
                    for (int i = 0; i < strsSize; i++)
                    {
                        String[] subStrs = strs[i].Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                        data.m_parameters[subStrs[0].ToLower()] = subStrs[1];
                    }
                }
                else
                {
                    data.m_url += "/" + parameters;
                }
                CIndicator indicator = null;
                if (nodeService.UseScript)
                {
                    try
                    {
                        lock (nodeService.m_indicators)
                        {
                            indicator = nodeService.m_indicators.Pop();
                        }
                    }
                    catch
                    {
                        indicator = CFunctionEx.CreateIndicator(nodeService.Script, nodeService.Native);
                    }
                    List <CFunction> functions = indicator.GetFunctions();
                    int functionsSize          = functions.Count;
                    for (int i = 0; i < functionsSize; i++)
                    {
                        CFunctionHttp function = functions[i] as CFunctionHttp;
                        if (function != null)
                        {
                            function.m_data = data;
                        }
                    }
                }
                data.m_socketID = newSocketID;
                lock (nodeService.m_httpDatas)
                {
                    nodeService.m_httpDatas[newSocketID] = data;
                }
                if (indicator != null)
                {
                    indicator.CallFunction("ONHTTPREQUEST();");
                }
                if (data.m_close)
                {
                    return;
                }
                int resContentLength = 0;
                if (data.m_resBytes != null)
                {
                    resContentLength = data.m_resBytes.Length;
                }
                else
                {
                    if (data.m_resStr != null)
                    {
                        resContentLength = Encoding.Default.GetBytes(data.m_resStr).Length;
                    }
                }
                StringBuilder bld = new StringBuilder();
                bld.Append("HTTP/1.0 " + data.m_statusCode.ToString() + " OK\r\n");
                bld.Append(String.Format("Content-Length: {0}\r\n", resContentLength));
                bld.Append("Connection: close\r\n\r\n");
                if (data.m_resBytes != null)
                {
                    socket.Send(Encoding.Default.GetBytes(bld.ToString()));
                    socket.Send(data.m_resBytes);
                }
                else
                {
                    bld.Append(data.m_resStr);
                    socket.Send(Encoding.Default.GetBytes(bld.ToString()));
                }
                if (indicator != null)
                {
                    lock (nodeService.m_indicators)
                    {
                        nodeService.m_indicators.Push(indicator);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
            finally
            {
                lock (nodeService.m_httpDatas)
                {
                    nodeService.m_httpDatas.Remove(newSocketID);
                }
                socket.Close();
            }
        }