Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/mount_node_http.cc

Issue 16959007: [NaCl SDK] httpfs now sets errno to an appropriate value on non-200 response. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Simplify code using HTTPStatusCodeToErrno. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | native_client_sdk/src/libraries/nacl_io_test/mount_http_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 /* Copyright (c) 2013 The Chromium 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 5
6 #include "nacl_io/mount_node_http.h" 6 #include "nacl_io/mount_node_http.h"
7 7
8 #include <assert.h> 8 #include <assert.h>
9 #include <errno.h> 9 #include <errno.h>
10 #include <stdio.h> 10 #include <stdio.h>
11 #include <string.h> 11 #include <string.h>
12 12
13 #include <ppapi/c/pp_errors.h> 13 #include <ppapi/c/pp_errors.h>
14 14
15 #include "nacl_io/mount_http.h" 15 #include "nacl_io/mount_http.h"
16 #include "nacl_io/osinttypes.h" 16 #include "nacl_io/osinttypes.h"
17 17
18 #if defined(WIN32) 18 #if defined(WIN32)
19 #define snprintf _snprintf 19 #define snprintf _snprintf
20 #endif 20 #endif
21 21
22 namespace { 22 namespace {
23 23
24 // If we're attempting to read a partial request, but the server returns a full 24 // If we're attempting to read a partial request, but the server returns a full
25 // request, we need to read all of the data up to the start of our partial 25 // request, we need to read all of the data up to the start of our partial
26 // request into a dummy buffer. This is the maximum size of that buffer. 26 // request into a dummy buffer. This is the maximum size of that buffer.
27 const size_t MAX_READ_BUFFER_SIZE = 64 * 1024; 27 const size_t MAX_READ_BUFFER_SIZE = 64 * 1024;
28 const int32_t STATUSCODE_OK = 200; 28 const int32_t STATUSCODE_OK = 200;
29 const int32_t STATUSCODE_PARTIAL_CONTENT = 206; 29 const int32_t STATUSCODE_PARTIAL_CONTENT = 206;
30 const int32_t STATUSCODE_FORBIDDEN = 403;
31 const int32_t STATUSCODE_NOT_FOUND = 404;
30 32
31 StringMap_t ParseHeaders(const char* headers, int32_t headers_length) { 33 StringMap_t ParseHeaders(const char* headers, int32_t headers_length) {
32 enum State { 34 enum State {
33 FINDING_KEY, 35 FINDING_KEY,
34 SKIPPING_WHITESPACE, 36 SKIPPING_WHITESPACE,
35 FINDING_VALUE, 37 FINDING_VALUE,
36 }; 38 };
37 39
38 StringMap_t result; 40 StringMap_t result;
39 std::string key; 41 std::string key;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 } else if (result == 3) { 119 } else if (result == 3) {
118 *read_start = read_start_int; 120 *read_start = read_start_int;
119 *read_end = read_end_int + 1; 121 *read_end = read_end_int + 1;
120 *entity_length = entity_length_int; 122 *entity_length = entity_length_int;
121 return true; 123 return true;
122 } 124 }
123 125
124 return false; 126 return false;
125 } 127 }
126 128
129 // Maps an HTTP |status_code| onto the appropriate errno code.
130 int HTTPStatusCodeToErrno(int status_code) {
131 switch (status_code) {
132 case STATUSCODE_OK:
133 case STATUSCODE_PARTIAL_CONTENT:
134 return 0;
135 case STATUSCODE_FORBIDDEN:
136 return EACCES;
137 case STATUSCODE_NOT_FOUND:
138 return ENOENT;
139 }
140 if (status_code >= 400 && status_code < 500)
141 return EINVAL;
142 return EIO;
143 }
144
127 } // namespace 145 } // namespace
128 146
129 void MountNodeHttp::SetCachedSize(off_t size) { 147 void MountNodeHttp::SetCachedSize(off_t size) {
130 has_cached_size_ = true; 148 has_cached_size_ = true;
131 stat_.st_size = size; 149 stat_.st_size = size;
132 } 150 }
133 151
134 Error MountNodeHttp::FSync() { return ENOSYS; } 152 Error MountNodeHttp::FSync() { return ENOSYS; }
135 153
136 Error MountNodeHttp::GetDents(size_t offs, 154 Error MountNodeHttp::GetDents(size_t offs,
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 // Get response statuscode. 309 // Get response statuscode.
292 PP_Var statuscode = response_interface->GetProperty( 310 PP_Var statuscode = response_interface->GetProperty(
293 response.pp_resource(), PP_URLRESPONSEPROPERTY_STATUSCODE); 311 response.pp_resource(), PP_URLRESPONSEPROPERTY_STATUSCODE);
294 312
295 if (statuscode.type != PP_VARTYPE_INT32) 313 if (statuscode.type != PP_VARTYPE_INT32)
296 return EINVAL; 314 return EINVAL;
297 315
298 *out_statuscode = statuscode.value.as_int; 316 *out_statuscode = statuscode.value.as_int;
299 317
300 // Only accept OK or Partial Content. 318 // Only accept OK or Partial Content.
301 if (*out_statuscode != STATUSCODE_OK && 319 Error error = HTTPStatusCodeToErrno(*out_statuscode);
302 *out_statuscode != STATUSCODE_PARTIAL_CONTENT) { 320 if (error)
303 return EINVAL; 321 return error;
304 }
305 322
306 // Get response headers. 323 // Get response headers.
307 PP_Var response_headers_var = response_interface->GetProperty( 324 PP_Var response_headers_var = response_interface->GetProperty(
308 response.pp_resource(), PP_URLRESPONSEPROPERTY_HEADERS); 325 response.pp_resource(), PP_URLRESPONSEPROPERTY_HEADERS);
309 326
310 uint32_t response_headers_length; 327 uint32_t response_headers_length;
311 const char* response_headers_str = 328 const char* response_headers_str =
312 var_interface->VarToUtf8(response_headers_var, &response_headers_length); 329 var_interface->VarToUtf8(response_headers_var, &response_headers_length);
313 330
314 *out_loader = loader.Release(); 331 *out_loader = loader.Release();
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 return PPErrorToErrno(bytes_read); 531 return PPErrorToErrno(bytes_read);
515 532
516 assert(bytes_read <= bytes_to_read); 533 assert(bytes_read <= bytes_to_read);
517 bytes_to_read -= bytes_read; 534 bytes_to_read -= bytes_read;
518 out_buffer += bytes_read; 535 out_buffer += bytes_read;
519 } 536 }
520 537
521 *out_bytes = count; 538 *out_bytes = count;
522 return 0; 539 return 0;
523 } 540 }
OLDNEW
« no previous file with comments | « no previous file | native_client_sdk/src/libraries/nacl_io_test/mount_http_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698