OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "update_engine/update_check_action.h" | 5 #include "update_engine/omaha_request_action.h" |
6 #include <inttypes.h> | 6 #include <inttypes.h> |
7 #include <sstream> | 7 #include <sstream> |
8 | 8 |
9 #include <libxml/parser.h> | 9 #include <libxml/parser.h> |
10 #include <libxml/xpath.h> | 10 #include <libxml/xpath.h> |
11 #include <libxml/xpathInternals.h> | 11 #include <libxml/xpathInternals.h> |
12 | 12 |
13 #include "chromeos/obsolete_logging.h" | 13 #include "chromeos/obsolete_logging.h" |
14 #include "update_engine/action_pipe.h" | 14 #include "update_engine/action_pipe.h" |
15 #include "update_engine/utils.h" | 15 #include "update_engine/utils.h" |
16 | 16 |
17 using std::string; | 17 using std::string; |
18 | 18 |
19 namespace chromeos_update_engine { | 19 namespace chromeos_update_engine { |
20 | 20 |
21 const char* const UpdateCheckParams::kAppId( | 21 const char* const OmahaRequestParams::kAppId( |
22 "{87efface-864d-49a5-9bb3-4b050a7c227a}"); | 22 "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
23 const char* const UpdateCheckParams::kOsPlatform("Chrome OS"); | 23 const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
24 const char* const UpdateCheckParams::kOsVersion("Indy"); | 24 const char* const OmahaRequestParams::kOsVersion("Indy"); |
25 const char* const UpdateCheckParams::kUpdateUrl( | 25 const char* const OmahaRequestParams::kUpdateUrl( |
26 "https://tools.google.com/service/update2"); | 26 "https://tools.google.com/service/update2"); |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0"); | 30 const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0"); |
31 | 31 |
32 // This is handy for passing strings into libxml2 | 32 // This is handy for passing strings into libxml2 |
33 #define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x)) | 33 #define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x)) |
34 | 34 |
35 // These are for scoped_ptr_malloc, which is like scoped_ptr, but allows | 35 // These are for scoped_ptr_malloc, which is like scoped_ptr, but allows |
(...skipping 16 matching lines...) Expand all Loading... |
52 xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x)); | 52 xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x)); |
53 } | 53 } |
54 }; | 54 }; |
55 class ScopedPtrXmlXPathContextFree { | 55 class ScopedPtrXmlXPathContextFree { |
56 public: | 56 public: |
57 inline void operator()(void* x) const { | 57 inline void operator()(void* x) const { |
58 xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x)); | 58 xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x)); |
59 } | 59 } |
60 }; | 60 }; |
61 | 61 |
62 // Returns a properly formatted omaha request for an update check | 62 // Returns a properly formatted omaha request for a request to Omaha. |
63 string FormatRequest(const UpdateCheckParams& params) { | 63 string FormatRequest(const OmahaRequestParams& params) { |
64 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | 64 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
65 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" " | 65 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" " |
66 "version=\"" + XmlEncode(kGupdateVersion) + "\" " | 66 "version=\"" + XmlEncode(kGupdateVersion) + "\" " |
67 "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" " | 67 "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" " |
68 "protocol=\"2.0\" " | 68 "protocol=\"2.0\" " |
69 "machineid=\"") + XmlEncode(params.machine_id) + "\" ismachine=\"1\" " | 69 "machineid=\"") + XmlEncode(params.machine_id) + "\" ismachine=\
"1\" " |
70 "userid=\"" + XmlEncode(params.user_id) + "\">\n" | 70 "userid=\"" + XmlEncode(params.user_id) + "\">\n" |
71 " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" + | 71 " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" + |
72 XmlEncode(params.os_platform) + "\" sp=\"" + | 72 XmlEncode(params.os_platform) + "\" sp=\"" + |
73 XmlEncode(params.os_sp) + "\"></o:os>\n" | 73 XmlEncode(params.os_sp) + "\"></o:os>\n" |
74 " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" + | 74 " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" + |
75 XmlEncode(params.app_version) + "\" " | 75 XmlEncode(params.app_version) + "\" " |
76 "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" + | 76 "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" + |
77 XmlEncode(params.app_track) + "\" board=\"" + | 77 XmlEncode(params.app_track) + "\" board=\"" + |
78 XmlEncode(params.os_board) + "\">\n" | 78 XmlEncode(params.os_board) + "\">\n" |
79 " <o:ping active=\"0\"></o:ping>\n" | 79 " <o:ping active=\"0\"></o:ping>\n" |
80 " <o:updatecheck></o:updatecheck>\n" | 80 " <o:updatecheck></o:updatecheck>\n" |
81 " </o:app>\n" | 81 " </o:app>\n" |
82 "</o:gupdate>\n"; | 82 "</o:gupdate>\n"; |
83 } | 83 } |
84 } // namespace {} | 84 } // namespace {} |
85 | 85 |
86 // Encodes XML entities in a given string with libxml2. input must be | 86 // Encodes XML entities in a given string with libxml2. input must be |
87 // UTF-8 formatted. Output will be UTF-8 formatted. | 87 // UTF-8 formatted. Output will be UTF-8 formatted. |
88 string XmlEncode(const string& input) { | 88 string XmlEncode(const string& input) { |
89 // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much | 89 // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much |
90 // // cpu, considering creating one and caching it. | 90 // // cpu, considering creating one and caching it. |
91 // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc( | 91 // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc( |
92 // xmlNewDoc(ConstXMLStr("1.0"))); | 92 // xmlNewDoc(ConstXMLStr("1.0"))); |
93 // if (!xml_doc.get()) { | 93 // if (!xml_doc.get()) { |
94 // LOG(ERROR) << "Unable to create xmlDoc"; | 94 // LOG(ERROR) << "Unable to create xmlDoc"; |
95 // return ""; | 95 // return ""; |
96 // } | 96 // } |
97 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( | 97 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
98 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str()))); | 98 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str()))); |
99 return string(reinterpret_cast<const char *>(str.get())); | 99 return string(reinterpret_cast<const char *>(str.get())); |
100 } | 100 } |
101 | 101 |
102 UpdateCheckAction::UpdateCheckAction(HttpFetcher* http_fetcher) | 102 OmahaRequestAction::OmahaRequestAction(HttpFetcher* http_fetcher) |
103 : http_fetcher_(http_fetcher) {} | 103 : http_fetcher_(http_fetcher) {} |
104 | 104 |
105 UpdateCheckAction::~UpdateCheckAction() {} | 105 OmahaRequestAction::~OmahaRequestAction() {} |
106 | 106 |
107 void UpdateCheckAction::PerformAction() { | 107 void OmahaRequestAction::PerformAction() { |
108 CHECK(HasInputObject()); | 108 CHECK(HasInputObject()); |
109 params_ = GetInputObject(); | 109 params_ = GetInputObject(); |
110 http_fetcher_->set_delegate(this); | 110 http_fetcher_->set_delegate(this); |
111 string request_post(FormatRequest(params_)); | 111 string request_post(FormatRequest(params_)); |
112 http_fetcher_->SetPostData(request_post.data(), request_post.size()); | 112 http_fetcher_->SetPostData(request_post.data(), request_post.size()); |
113 LOG(INFO) << "Checking for update at " << params_.update_url; | 113 LOG(INFO) << "Checking for update at " << params_.update_url; |
114 LOG(INFO) << "Request: " << request_post; | 114 LOG(INFO) << "Request: " << request_post; |
115 http_fetcher_->BeginTransfer(params_.update_url); | 115 http_fetcher_->BeginTransfer(params_.update_url); |
116 } | 116 } |
117 | 117 |
118 void UpdateCheckAction::TerminateProcessing() { | 118 void OmahaRequestAction::TerminateProcessing() { |
119 http_fetcher_->TerminateTransfer(); | 119 http_fetcher_->TerminateTransfer(); |
120 } | 120 } |
121 | 121 |
122 // We just store the response in the buffer. Once we've received all bytes, | 122 // We just store the response in the buffer. Once we've received all bytes, |
123 // we'll look in the buffer and decide what to do. | 123 // we'll look in the buffer and decide what to do. |
124 void UpdateCheckAction::ReceivedBytes(HttpFetcher *fetcher, | 124 void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher, |
125 const char* bytes, | 125 const char* bytes, |
126 int length) { | 126 int length) { |
127 response_buffer_.reserve(response_buffer_.size() + length); | 127 response_buffer_.reserve(response_buffer_.size() + length); |
128 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length); | 128 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length); |
129 } | 129 } |
130 | 130 |
131 namespace { | 131 namespace { |
132 // If non-NULL response, caller is responsible for calling xmlXPathFreeObject() | 132 // If non-NULL response, caller is responsible for calling xmlXPathFreeObject() |
133 // on the returned object. | 133 // on the returned object. |
134 // This code is roughly based on the libxml tutorial at: | 134 // This code is roughly based on the libxml tutorial at: |
135 // http://xmlsoft.org/tutorial/apd.html | 135 // http://xmlsoft.org/tutorial/apd.html |
136 xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath, | 136 xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 // failure | 185 // failure |
186 return 0; | 186 return 0; |
187 } | 187 } |
188 return ret; | 188 return ret; |
189 } | 189 } |
190 } // namespace {} | 190 } // namespace {} |
191 | 191 |
192 // If the transfer was successful, this uses libxml2 to parse the response | 192 // If the transfer was successful, this uses libxml2 to parse the response |
193 // and fill in the appropriate fields of the output object. Also, notifies | 193 // and fill in the appropriate fields of the output object. Also, notifies |
194 // the processor that we're done. | 194 // the processor that we're done. |
195 void UpdateCheckAction::TransferComplete(HttpFetcher *fetcher, | 195 void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher, |
196 bool successful) { | 196 bool successful) { |
197 ScopedActionCompleter completer(processor_, this); | 197 ScopedActionCompleter completer(processor_, this); |
198 LOG(INFO) << "Update check response: " << string(response_buffer_.begin(), | 198 LOG(INFO) << "Update check response: " << string(response_buffer_.begin(), |
199 response_buffer_.end()); | 199 response_buffer_.end()); |
200 if (!successful) { | 200 if (!successful) { |
201 LOG(ERROR) << "Update check network transfer failed."; | 201 LOG(ERROR) << "Update check network transfer failed."; |
202 return; | 202 return; |
203 } | 203 } |
204 if (!HasOutputPipe()) { | 204 if (!HasOutputPipe()) { |
205 // Just set success to whether or not the http transfer succeeded, | 205 // Just set success to whether or not the http transfer succeeded, |
206 // which must be true at this point in the code. | 206 // which must be true at this point in the code. |
(...skipping 26 matching lines...) Expand all Loading... |
233 CHECK_GE(nodeset->nodeNr, 1); | 233 CHECK_GE(nodeset->nodeNr, 1); |
234 | 234 |
235 xmlNode* updatecheck_node = nodeset->nodeTab[0]; | 235 xmlNode* updatecheck_node = nodeset->nodeTab[0]; |
236 // get status | 236 // get status |
237 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) { | 237 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) { |
238 LOG(ERROR) << "Response missing status"; | 238 LOG(ERROR) << "Response missing status"; |
239 return; | 239 return; |
240 } | 240 } |
241 | 241 |
242 const string status(XmlGetProperty(updatecheck_node, "status")); | 242 const string status(XmlGetProperty(updatecheck_node, "status")); |
243 UpdateCheckResponse output_object; | 243 OmahaResponse output_object; |
244 if (status == "noupdate") { | 244 if (status == "noupdate") { |
245 LOG(INFO) << "No update."; | 245 LOG(INFO) << "No update."; |
246 output_object.update_exists = false; | 246 output_object.update_exists = false; |
247 SetOutputObject(output_object); | 247 SetOutputObject(output_object); |
248 completer.set_success(true); | 248 completer.set_success(true); |
249 return; | 249 return; |
250 } | 250 } |
251 | 251 |
252 if (status != "ok") { | 252 if (status != "ok") { |
253 LOG(ERROR) << "Unknown status: " << status; | 253 LOG(ERROR) << "Unknown status: " << status; |
(...skipping 12 matching lines...) Expand all Loading... |
266 output_object.hash = XmlGetProperty(updatecheck_node, "hash"); | 266 output_object.hash = XmlGetProperty(updatecheck_node, "hash"); |
267 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size")); | 267 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size")); |
268 output_object.needs_admin = | 268 output_object.needs_admin = |
269 XmlGetProperty(updatecheck_node, "needsadmin") == "true"; | 269 XmlGetProperty(updatecheck_node, "needsadmin") == "true"; |
270 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true"; | 270 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true"; |
271 SetOutputObject(output_object); | 271 SetOutputObject(output_object); |
272 return; | 272 return; |
273 } | 273 } |
274 | 274 |
275 }; // namespace chromeos_update_engine | 275 }; // namespace chromeos_update_engine |
OLD | NEW |