1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CLOUD_PRINT_GCP20_PROTOTYPE_PRIVET_HTTP_SERVER_H_ 6 #define CLOUD_PRINT_GCP20_PROTOTYPE_PRIVET_HTTP_SERVER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/values.h" 13 #include "net/http/http_status_code.h" 14 #include "net/server/http_server.h" 15 #include "net/server/http_server_request_info.h" 16 17 class GURL; 18 19 // HTTP server for receiving . 20 class PrivetHttpServer: public net::HttpServer::Delegate { 21 public: 22 // TODO(maksymb): Move this enum to some namespace instead of this class. 23 enum RegistrationErrorStatus { 24 REG_ERROR_OK, 25 26 REG_ERROR_INVALID_PARAMS, 27 REG_ERROR_DEVICE_BUSY, 28 REG_ERROR_PENDING_USER_ACTION, 29 REG_ERROR_USER_CANCEL, 30 REG_ERROR_CONFIRMATION_TIMEOUT, 31 REG_ERROR_INVALID_ACTION, 32 REG_ERROR_SERVER_ERROR 33 }; 34 35 // TODO(maksymb): Move this struct to some namespace instead of this class. 36 struct DeviceInfo { 37 DeviceInfo(); 38 ~DeviceInfo(); 39 40 std::string version; 41 std::string name; 42 std::string description; 43 std::string url; 44 std::string id; 45 std::string device_state; 46 std::string connection_state; 47 std::string manufacturer; 48 std::string model; 49 std::string serial_number; 50 std::string firmware; 51 int uptime; 52 std::string x_privet_token; 53 54 std::vector<std::string> api; 55 std::vector<std::string> type; 56 }; 57 58 class Delegate { 59 public: 60 Delegate() {} 61 62 virtual ~Delegate() {} 63 64 // Invoked when registration is starting. 65 virtual RegistrationErrorStatus RegistrationStart( 66 const std::string& user) = 0; 67 68 // Invoked when claimtoken is needed. 69 virtual RegistrationErrorStatus RegistrationGetClaimToken( 70 const std::string& user, 71 std::string* token, 72 std::string* claim_url) = 0; 73 74 // Invoked when registration is going to be completed. 75 virtual RegistrationErrorStatus RegistrationComplete( 76 const std::string& user, 77 std::string* device_id) = 0; 78 79 // Invoked when client asked for cancelling the registration. 80 virtual RegistrationErrorStatus RegistrationCancel( 81 const std::string& user) = 0; 82 83 // Invoked for receiving server error details. 84 virtual void GetRegistrationServerError(std::string* description) = 0; 85 86 // Invoked when /privet/info is called. 87 virtual void CreateInfo(DeviceInfo* info) = 0; 88 89 // Invoked for checking should /privet/register be exposed. 90 virtual bool IsRegistered() const = 0; 91 92 // Invoked when XPrivetToken has to be checked. 93 virtual bool CheckXPrivetTokenHeader(const std::string& token) const = 0; 94 }; 95 96 // Constructor doesn't start server. 97 explicit PrivetHttpServer(Delegate* delegate); 98 99 // Destroys the object. 100 virtual ~PrivetHttpServer(); 101 102 // Starts HTTP server: start listening port |port| for HTTP requests. 103 bool Start(uint16 port); 104 105 // Stops HTTP server. 106 void Shutdown(); 107 108 private: 109 // net::HttpServer::Delegate methods: 110 virtual void OnHttpRequest( 111 int connection_id, 112 const net::HttpServerRequestInfo& info) OVERRIDE; 113 virtual void OnWebSocketRequest( 114 int connection_id, 115 const net::HttpServerRequestInfo& info) OVERRIDE; 116 virtual void OnWebSocketMessage(int connection_id, 117 const std::string& data) OVERRIDE; 118 virtual void OnClose(int connection_id) OVERRIDE; 119 120 // Sends error as response. Invoked when request method is invalid. 121 void ReportInvalidMethod(int connection_id); 122 123 // Returns |true| if |request| should be done with correct |method|. 124 // Otherwise sends |Invalid method| error. 125 // Also checks support of |request| by this server. 126 bool ValidateRequestMethod(int connection_id, 127 const std::string& request, 128 const std::string& method); 129 130 // Processes http request after all preparations (XPrivetHeader check, 131 // data handling etc.) 132 net::HttpStatusCode ProcessHttpRequest(const GURL& url, 133 const std::string& data, 134 std::string* response); 135 136 // Pivet API methods. Return reference to NULL if output should be empty. 137 scoped_ptr<base::DictionaryValue> ProcessInfo( 138 net::HttpStatusCode* status_code) const; 139 scoped_ptr<base::DictionaryValue> ProcessReset( 140 net::HttpStatusCode* status_code); 141 scoped_ptr<base::DictionaryValue> ProcessRegister( 142 const GURL& url, 143 net::HttpStatusCode* status_code); 144 145 // Proccesses current status and depending on it replaces (or not) 146 // |current_response| with error or empty response. 147 void ProcessRegistrationStatus( 148 RegistrationErrorStatus status, 149 scoped_ptr<base::DictionaryValue>* current_response) const; 150 151 // Port for listening. 152 153 uint16 port_; 154 155 // Contains encapsulated object for listening for requests. 156 scoped_refptr<net::HttpServer> server_; 157 158 Delegate* delegate_; 159 160 DISALLOW_COPY_AND_ASSIGN(PrivetHttpServer); 161 }; 162 163 #endif // CLOUD_PRINT_GCP20_PROTOTYPE_PRIVET_HTTP_SERVER_H_ 164 165