OLD | NEW |
| (Empty) |
1 // Copyright 2008-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/tools/omahacompatibility/httpserver/update_check_handler.h" | |
17 #include <windows.h> | |
18 #include "omaha/common/debug.h" | |
19 #include "omaha/common/logging.h" | |
20 #include "omaha/goopdate/goopdate_utils.h" | |
21 #include "omaha/tools/omahacompatibility/common/error.h" | |
22 #include "omaha/tools/omahacompatibility/httpserver/request.h" | |
23 #include "omaha/tools/omahacompatibility/httpserver/xml_parser.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 UpdateCheckHandler::UpdateCheckHandler(const CString& url_path, | |
28 PingObserver* observer) | |
29 : UrlHandler(url_path), | |
30 ping_observer_(observer) { | |
31 CORE_LOG(L1, (_T("[UpdateCheckHandler]"))); | |
32 } | |
33 | |
34 // Private method that goes over the request, i.e. the update checks or | |
35 // the pings for each application and constructs an appropriate response. | |
36 // For pings we dont do anything, and for update checks we construct the | |
37 // update response based on the config information. | |
38 HRESULT UpdateCheckHandler::BuildResponse(const AppRequestDataVector& request, | |
39 ServerResponses* responses) { | |
40 ASSERT1(responses); | |
41 | |
42 for (size_t i = 0; i < request.size(); ++i) { | |
43 AppData app_data = request[i].app_data(); | |
44 ServerResponse response; | |
45 response.guid = GuidToString(app_data.app_guid()); | |
46 | |
47 if (request[i].num_ping_events() > 0) { | |
48 // This is a ping request. We only handle the ping and nothing else. | |
49 response.is_ping = true; | |
50 if (ping_observer_) { | |
51 ping_observer_->Observe(request[i]); | |
52 } | |
53 } else { | |
54 // We assume this is an update check and send back an appropriate | |
55 // response. | |
56 ConfigResponse config_app_response; | |
57 HRESULT hr = FindResponse(app_data.app_guid(), | |
58 app_data.version(), | |
59 app_data.ap(), | |
60 &config_app_response); | |
61 if (SUCCEEDED(hr)) { | |
62 response.is_update_response = true; | |
63 response.response_data.set_url(config_app_response.url); | |
64 response.response_data.set_hash(config_app_response.hash); | |
65 response.response_data.set_needs_admin(config_app_response.needs_admin ? | |
66 omaha::NEEDS_ADMIN_YES : | |
67 omaha::NEEDS_ADMIN_NO); | |
68 response.response_data.set_size(config_app_response.size); | |
69 } | |
70 // Continuing here in the failed case will cause the | |
71 // is_update_response to be false, | |
72 // causing the server to respond with no-update. | |
73 } | |
74 responses->push_back(response); | |
75 } | |
76 | |
77 return S_OK; | |
78 } | |
79 | |
80 HRESULT UpdateCheckHandler::HandleRequest(const HttpRequest& http_request, | |
81 HttpResponse* response) { | |
82 CORE_LOG(L1, (_T("[UpdateCheckHandler::HandleRequest]"))); | |
83 ASSERT1(response); | |
84 ASSERT1(http_request.http_verb() == HttpVerbPOST); | |
85 | |
86 // Parse the request. | |
87 CORE_LOG(L1, (_T("[Request %s]"), http_request.content())); | |
88 AppRequestDataVector request; | |
89 HRESULT hr = ParseUpdateCheck(http_request.content(), &request); | |
90 if (FAILED(hr)) { | |
91 CORE_LOG(LW, (_T("[ParseUpdateCheck failed]"))); | |
92 return hr; | |
93 } | |
94 | |
95 // Map the request to a response. | |
96 ServerResponses responses; | |
97 hr = BuildResponse(request, &responses); | |
98 if (FAILED(hr)) { | |
99 return hr; | |
100 } | |
101 | |
102 // Convert the response into the response string. | |
103 CString update_response_str; | |
104 hr = BuildUpdateResponse(responses, &update_response_str); | |
105 if (FAILED(hr)) { | |
106 CORE_LOG(LW, (_T("[BuildUpdateResponse failed]"))); | |
107 return hr; | |
108 } | |
109 | |
110 CORE_LOG(L1, (_T("[Response %s]"), update_response_str)); | |
111 response->set_response_str(update_response_str); | |
112 | |
113 return S_OK; | |
114 } | |
115 | |
116 HRESULT UpdateCheckHandler::AddAppVersionResponse( | |
117 const ConfigResponse& response) { | |
118 CORE_LOG(L1, (_T("[UpdateCheckHandler::AddAppVersionResponse]"))); | |
119 // TODO(omaha): Add duplicate checking. | |
120 | |
121 config_responses_.push_back(response); | |
122 return S_OK; | |
123 } | |
124 | |
125 HRESULT UpdateCheckHandler::FindResponse(GUID guid, | |
126 const CString& version, | |
127 const CString& ap, | |
128 ConfigResponse* response) { | |
129 CORE_LOG(L1, (_T("[UpdateCheckHandler::FindResponse]"))); | |
130 ASSERT1(response); | |
131 | |
132 // The idea here is that we go over the config responses, for a request | |
133 // that contains 0.0.0.0 in the request, or empty we return the initial | |
134 // response, i.e. the first installer we read. | |
135 // If the version is not empty, then we match it with the previous config | |
136 // version, i.e. the version that should have been installed, | |
137 // and return a response if there is a match. | |
138 if (version.IsEmpty() || version == _T("0.0.0.0")) { | |
139 // This is the initial response. | |
140 *response = config_responses_[0]; | |
141 return S_OK; | |
142 } | |
143 | |
144 for (size_t i = 1; i < config_responses_.size(); ++i) { | |
145 if (::IsEqualGUID(config_responses_[i].guid, guid) && | |
146 _T("update_app") == ap && | |
147 version == config_responses_[i - 1].version) { | |
148 // This is the second response. | |
149 *response = config_responses_[i]; | |
150 return S_OK; | |
151 } | |
152 } | |
153 | |
154 return E_FAIL; | |
155 } | |
156 | |
157 } // namespace omaha | |
OLD | NEW |