1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 #include "pvmf_protocol_engine_download_common.h"
19
20 ////////////////////////////////////////////////////////////////////////////////////
21 ////// DownloadState related implementation
22 ////////////////////////////////////////////////////////////////////////////////////
processMicroStateSendRequestPreCheck()23 OSCL_EXPORT_REF int32 DownloadState::processMicroStateSendRequestPreCheck()
24 {
25 if (!iCfgFile.GetRep()) return PROCESS_INPUT_OUTPUT_NOT_READY;
26 return ProtocolState::processMicroStateSendRequestPreCheck();
27 }
28
processMicroStateGetResponsePreCheck()29 OSCL_EXPORT_REF int32 DownloadState::processMicroStateGetResponsePreCheck()
30 {
31 int32 status = ProtocolState::processMicroStateGetResponsePreCheck();
32 if (status != PROCESS_SUCCESS) return status;
33
34 // reset
35 iOutputDataQueue.clear();
36
37 // register observer and data queue to be used in iParser
38 iParser->registerObserverAndOutputQueue(this, &iOutputDataQueue);
39
40 return PROCESS_SUCCESS;
41 }
42
setRequestBasics()43 OSCL_EXPORT_REF void DownloadState::setRequestBasics()
44 {
45 iComposer->setMethod(HTTP_METHOD_GET);
46 //iComposer->setVersion(HTTP_V1_1);
47 iComposer->setVersion((HTTPVersion)iCfgFile->getHttpVersion());
48 StrPtrLen uri((iURI.getURI()).get_cstr(), (iURI.getURI()).get_size());
49 iComposer->setURI(uri);
50 }
51
52 // For composing a request, only need to override this function
setHeaderFields()53 OSCL_EXPORT_REF bool DownloadState::setHeaderFields()
54 {
55 // set fields
56 OSCL_FastString fieldKeyString(_STRLIT_CHAR("Host"));
57 StrCSumPtrLen fieldKey(fieldKeyString.get_cstr());
58 if (!iComposer->setField(fieldKey, iURI.getHost().get_cstr())) return false;
59
60 fieldKeyString.set((OSCL_String::chartype*)_STRLIT_CHAR("User-Agent"), 16);
61 fieldKey.setPtrLen(fieldKeyString.get_str(), fieldKeyString.get_size());
62 if (!iComposer->setField(fieldKey, (iCfgFile->GetUserAgent()).get_cstr())) return false;
63
64 fieldKeyString.set((OSCL_String::chartype*)_STRLIT_CHAR("Connection"), 16);
65 OSCL_FastString fieldValueString(_STRLIT_CHAR("Keep-Alive"));
66 fieldKey.setPtrLen(fieldKeyString.get_cstr(), fieldKeyString.get_size());
67 if (!iComposer->setField(fieldKey, fieldValueString.get_cstr())) return false;
68
69 return true;
70 }
71
72 // shared routine for all the download protocols
checkParsingStatus(int32 parsingStatus)73 OSCL_EXPORT_REF int32 DownloadState::checkParsingStatus(int32 parsingStatus)
74 {
75 if (parsingStatus == HttpParsingBasicObject::PARSE_CONTENT_RANGE_INFO_NOT_MATCH)
76 {
77 return PROCESS_CONTENT_RANGE_INFO_NOT_MATCH;
78 }
79 else if (parsingStatus == HttpParsingBasicObject::PARSE_CONTENT_LENGTH_NOT_MATCH)
80 {
81 return PROCESS_CONTENT_LENGTH_NOT_MATCH;
82 }
83
84 return ProtocolState::checkParsingStatus(parsingStatus);
85 }
86
updateDownloadStatistics()87 OSCL_EXPORT_REF int32 DownloadState::updateDownloadStatistics()
88 {
89 // update current file size in config file
90 uint32 downloadSize = iParser->getDownloadSize();
91 bool aFirstDownloadFromFileBeginning = (downloadSize > 0 && iCfgFile->GetCurrentFileSize() == 0);
92 if (downloadSize > iCfgFile->GetCurrentFileSize())
93 {
94 iCfgFile->SetCurrentFileSize(downloadSize);
95 // set ContentLengthFlag to ConfigFile object
96 if (!iSetContentLengthFlagtoConfigFileObject)
97 {
98 iCfgFile->setHasContentLengthFlag((iParser->getContentLength() > 0));
99 iSetContentLengthFlagtoConfigFileObject = true;
100 }
101 }
102
103 // update total file size in config file
104 uint32 contentLength = iParser->getContentLength();
105 if (iCfgFile->GetOverallFileSize() == 0 ||
106 (iCfgFile->GetOverallFileSize() != contentLength && contentLength > 0))
107 {
108 iCfgFile->SetOverallFileSize(contentLength);
109 }
110 //else if(contentLength == 0 && downloadSize >= iCfgFile->GetOverallFileSize()) {
111 else if (contentLength == 0)
112 {
113 //iCfgFile->SetOverallFileSize(downloadSize);
114 if (downloadSize > iCfgFile->GetMaxAllowedFileSize())
115 {
116 // file has to be truncated due to request size limition
117 iParser->setDownloadSize(iCfgFile->GetMaxAllowedFileSize());
118 iCfgFile->SetOverallFileSize(iCfgFile->GetMaxAllowedFileSize());
119 iCfgFile->SetCurrentFileSize(iCfgFile->GetMaxAllowedFileSize());
120 return PROCESS_SUCCESS_END_OF_MESSAGE_TRUNCATED;
121 }
122 }
123
124 // save to config file for the first download from beginning of the file
125 if (aFirstDownloadFromFileBeginning) saveConfig();
126 return PROCESS_SUCCESS;
127 }
128
saveConfig()129 OSCL_EXPORT_REF void DownloadState::saveConfig()
130 {
131 iCfgFile->SaveConfig();
132 }
133
134