symbian http 连接 源码 代码

#include "m5httpdown.h" #include <e32std.h> #include <f32file.h> _LIT(KCMCCWapProxy, "10.0.0.172") ; CM5HttpDown::CM5HttpDown(M5HttpDownNotifier & m5_notifier):m_m5_notifier(m5_notifier) { m_sock_eng = NULL ; } CM5HttpDown::~CM5HttpDown() { if(m_sock_eng->IsActive()) { m_sock_eng->Disconnect() ; } delete m_sock_eng ; } void CM5HttpDown::ConstructL() { m_running = EFalse ; m_down_type = HTTP_DOWN_CMWAP ; m_is_first_resp = ETrue ; m_total_bytes = 0 ; m_recv_bytes = 0 ; m_web_port = HTTP_WEB_PORT ; m_web_addr.SetLength(0) ; m_web_fname.SetLength(0) ; m_sock_eng = CSocketsEngine::NewL(*this) ; } CM5HttpDown * CM5HttpDown::NewL(M5HttpDownNotifier& m5_notifier) { CM5HttpDown * self = CM5HttpDown::NewLC(m5_notifier); CleanupStack::Pop(self); return self; } CM5HttpDown * CM5HttpDown::NewLC(M5HttpDownNotifier& m5_notifier) { CM5HttpDown * self = new (ELeave) CM5HttpDown(m5_notifier); CleanupStack::PushL(self); self->ConstructL(); return self; } void CM5HttpDown::PrintNotify(const TDesC& aMessage, TUint aAttributes) { m_m5_notifier.M5PrintNotify(aMessage) ; } TBool CM5HttpDown::CheckRecv(const TDesC8& recv_buf) { TInt find_pos ; find_pos = recv_buf.Find(m_web_addr) ; if(find_pos != KErrNotFound) return EFalse ; return ETrue ; } void CM5HttpDown::RecvNotify(const TDesC8& aMessage) { TInt recv_bytes = aMessage.Length() ; TInt jump_length = 0 ; if(recv_bytes > 0) { if(m_is_first_resp) { if(CheckRecv(aMessage)) { if(ParseWebFileInfo(aMessage, m_total_bytes, jump_length)) { if(m_recv_bytes > 0) m_total_bytes += m_recv_bytes ; m_recv_bytes += (recv_bytes - jump_length) ; m_m5_notifier.M5RecvNotify(aMessage.Mid(jump_length)) ; } m_is_first_resp = false ; } else { TBuf<20> s ; s.Format(_L("check failed !")) ; PrintNotify(s) ; } } else { // common receive procdure if((m_recv_bytes + recv_bytes) > m_total_bytes) { recv_bytes = m_total_bytes - m_recv_bytes ; m_recv_bytes = m_total_bytes ; } else { m_recv_bytes += recv_bytes ; } m_m5_notifier.M5RecvNotify(aMessage) ; } } } void CM5HttpDown::ErrorNotify(const TDesC& aErrMessage, TInt aErrCode) { m_m5_notifier.M5PrintNotify(aErrMessage) ; } void CM5HttpDown::SetStatus(const TDesC& aStatus) { m_m5_notifier.M5PrintNotify(aStatus) ; } TBool CM5HttpDown::SendReq(TDesC8& req_str) { if(m_sock_eng->Connected()) { m_sock_eng->WriteL(req_str) ; return ETrue ; } return EFalse ; } TBool CM5HttpDown::ParseUri(TDesC8& uri, TDes8& web_addr, TDes8& web_fname, TInt& web_port) { TPtrC8 uri_ptr ; TBuf8<30> tmp_buf ; TInt find_pos = 0 ; web_port = HTTP_WEB_PORT ; if(uri.Length() <= 0) return false ; // search and jump over the http or https prefix uri_ptr.Set(uri.Ptr(), uri.Length()) ; find_pos = uri.Find(KHttpPrefix) ; if(find_pos != KErrNotFound) { tmp_buf.Copy(KHttpPrefix) ; uri_ptr.Set(uri.Ptr()+tmp_buf.Length(), uri.Length() - tmp_buf.Length()) ; } else { find_pos = uri.Find(KHttpsPrefix) ; if(find_pos != KErrNotFound) { tmp_buf.Copy(KHttpsPrefix) ; uri_ptr.Set(uri.Ptr()+tmp_buf.Length(), uri.Length() - tmp_buf.Length()) ; } } // get web address find_pos = uri_ptr.Find(KHttpClip) ; if(find_pos != KErrNotFound) { web_addr.Copy(uri_ptr.Mid(0, find_pos)) ; } // get web file name m_web_fname.Copy(uri_ptr.Ptr() + find_pos, uri_ptr.Length() - find_pos) ; return ETrue ; } TBool CM5HttpDown::GetRespField(const TDesC8& recv_buf, TDesC8& field_name, TDesC8& end_flag, TDes8& res) { TPtrC8 ptr_hdr ; TInt find_pos ; if(recv_buf.Length() <= 0) return EFalse ; find_pos = recv_buf.Find(field_name) ; if(find_pos != KErrNotFound) { ptr_hdr.Set(recv_buf.Ptr() + find_pos + field_name.Length(), recv_buf.Length() - find_pos - field_name.Length()) ; find_pos = ptr_hdr.Find(end_flag) ; if(find_pos != KErrNotFound) { res.Copy(ptr_hdr.Ptr(), find_pos) ; } return ETrue ; } return EFalse ; } TBool CM5HttpDown::ParseWebFileInfo(const TDesC8& recv_buf, TInt& file_length, TInt& jump_len) { TBuf8<30> tmp_field ; TBuf8<30> tmp_end ; TBuf8<30> tmp_str ; int find_pos ; file_length = 0 ; jump_len = 0 ; // get web file length if(m_down_type == HTTP_DOWN_CMWAP) { tmp_field.Append(KHttpCMWapHdrFileLen) ; } else { tmp_field.Append(KHttpCMNetHdrFileLen) ; } tmp_end.Append(KHttpHdrDiv) ; if(!GetRespField(recv_buf, tmp_field, tmp_end, tmp_str)) return EFalse ; file_length = Str2Int(tmp_str) ; // set the jump length find_pos = recv_buf.Find(KHttpHdrEnd) ; if(find_pos != KErrNotFound) { tmp_str.Copy(KHttpHdrEnd) ; jump_len = find_pos + tmp_str.Length() ; } return ETrue ; } TBool CM5HttpDown::InitSock(TDesC8& server_name, TInt server_port) { TBuf<50> svr_name ; svr_name.Copy(server_name) ; m_sock_eng->SetServerName(svr_name) ; m_sock_eng->SetPort(server_port) ; m_sock_eng->ConnectL() ; return ETrue ; } TBool CM5HttpDown::CloseSock() { if(m_running && m_sock_eng->IsActive()) { m_sock_eng->Disconnect() ; } return true ; } TInt CM5HttpDown::Str2Int(const TDesC8 & s) { TInt value = 0 ; TLex8 lex(s) ; lex.Val(value) ; return value ; } TBool CM5HttpDown::HttpConnPorxy(TDesC8& uri, TInt down_type) { m_running = true ; m_down_type = down_type ; ParseUri(uri, m_web_addr, m_web_fname, m_web_port) ; if(m_down_type == HTTP_DOWN_CMWAP) { TBuf8<20> proxy_svr ; proxy_svr.Copy(KCMCCWapProxy) ; if(!InitSock(proxy_svr, 80)) return EFalse ; } else { if(!InitSock(m_web_addr, m_web_port)) return EFalse ; } return ETrue ; } TBool CM5HttpDown::HttpDown(TDesC8& uri, TInt recv_bytes) { TBuf8<20> tmp_str ; m_recv_bytes = recv_bytes ; m_send_buf.SetLength(0) ; if(m_recv_bytes == 0) { m_send_buf.Append(KHttpCommonGet1) ; if(m_down_type == HTTP_DOWN_CMWAP) m_send_buf.Append(uri) ; else m_send_buf.Append(m_web_fname) ; m_send_buf.Append(KHttpCommonGet2) ; m_send_buf.Append(m_web_addr) ; m_send_buf.Append(KHttpCommonGet3) ; tmp_str.Format(_L8("%d"), m_web_port) ; m_send_buf.Append(tmp_str) ; m_send_buf.Append(KHttpCommonGet4) ; } else { m_send_buf.Append(KHttpResumeGet1) ; if(m_down_type == HTTP_DOWN_CMWAP) m_send_buf.Append(uri) ; else m_send_buf.Append(m_web_fname) ; m_send_buf.Append(KHttpResumeGet2) ; m_send_buf.Append(m_web_addr) ; m_send_buf.Append(KHttpResumeGet3) ; tmp_str.Format(_L8("%d"), m_web_port) ; m_send_buf.Append(tmp_str) ; m_send_buf.Append(KHttpResumeGet4) ; tmp_str.Format(_L8("%d"), m_recv_bytes) ; m_send_buf.Append(tmp_str) ; m_send_buf.Append(KHttpResumeGet5) ; } // send the request return SendReq(m_send_buf) ; } TBool CM5HttpDown::HttpStopDown() { return CloseSock() ; }  

 

#ifndef _M5_HTTP_DOWN_H_ #include #include #include "socketsengine.h" #include "uinotifier.h" #include "m5httpdownnotifier.h" #define HTTP_WEB_PORT 80 #define HTTP_TEMP_BUF_LEN 120 #define HTTP_SEND_BUF_LEN 256 #ifndef HTTP_DOWN_CMWAP #define HTTP_DOWN_CMWAP 0 #define HTTP_DOWN_CMNET 1 #endif _LIT8(KHttpRespOK, "200 OK") ; _LIT8(KHttpCMWapHdrFileLen, "Content-length: ") ; _LIT8(KHttpCMNetHdrFileLen, "Content-Length: ") ; _LIT8(KHttpClip, "/") ; _LIT8(KHttpHdrDiv, "/r/n") ; _LIT8(KHttpHdrEnd, "/r/n/r/n") ; _LIT8(KHttpPrefix, "http://") ; _LIT8(KHttpsPrefix, "https://") ; _LIT8(KHttpCommonGet1, "GET ") ; _LIT8(KHttpCommonGet2, " HTTP/1.1/r/nUser-Agent: Nokia 7610/r/nHost: ") ; _LIT8(KHttpCommonGet3, ":") ; _LIT8(KHttpCommonGet4, "/r/nAccept: */*/r/nConnection: Keep-Alive/r/n/r/n") ; _LIT8(KHttpResumeGet1, "GET ") ; _LIT8(KHttpResumeGet2, " HTTP/1.1/r/nUser-Agent: Nokia 7610/r/nHost: ") ; _LIT8(KHttpResumeGet3, ":") ; _LIT8(KHttpResumeGet4, "/r/nAccept: */*/r/nRANGE: bytes=") ; _LIT8(KHttpResumeGet5, "-/r/nConnection: Keep-Alive/r/n/r/n") ; class CM5HttpDown : public CBase, public MUINotifier { protected: // socket data TInt m_down_type ; CSocketsEngine * m_sock_eng ; TBool m_running ; TBool m_is_first_resp ; TInt m_web_port ; TInt m_total_bytes ; TInt m_recv_bytes; TBuf8 m_send_buf ; TBuf8 m_web_addr ; TBuf8 m_web_fname ; M5HttpDownNotifier& m_m5_notifier ; public: // MUINotifier implements void PrintNotify(const TDesC& aMessage, TUint aAttributes = 0) ; void RecvNotify(const TDesC8& aMessage) ; void ErrorNotify(const TDesC& aErrMessage, TInt aErrCode) ; void SetStatus(const TDesC& aStatus) ; protected: TInt Str2Int(const TDesC8 & s) ; TBool CheckRecv(const TDesC8& recv_buf) ; TBool ParseUri(TDesC8& uri, TDes8& web_addr, TDes8& web_fname, TInt& web_port) ; TBool ParseWebFileInfo(const TDesC8& recv_buf, TInt& total_length, TInt& jump_len) ; TBool GetRespField(const TDesC8& recv_buf, TDesC8& field_name, TDesC8& end_flag, TDes8& res) ; TBool InitSock(TDesC8& server_name, TInt server_port) ; TBool SendReq(TDesC8& req_str) ; TBool CloseSock() ; private: CM5HttpDown(M5HttpDownNotifier & m5_notifier) ; void ConstructL(TInt down_type) ; public: ~CM5HttpDown() ; static CM5HttpDown * NewL(M5HttpDownNotifier& m5_notifie, TInt down_type) ; static CM5HttpDown * NewLC(M5HttpDownNotifier& m5_notifier, TInt down_type) ; TBool IsRunning() {return m_running ; } TInt HttpTotalSize() { return m_total_bytes ; } TInt HttpRecvSize() { return m_recv_bytes ; } TBool HttpConnPorxy(TDesC8& uri) ; TBool HttpDown(TDesC8& uri, TInt recv_bytes = 0) ; TBool HttpStopDown() ; } ; #endif  

你可能感兴趣的:(symbian http 连接 源码 代码)