static ConnInterface::Ptr url_parse_serial( std::string path, int baud, std::string query) { std::string file_path; int baudrate; // /dev/ttyACM0:57600 url_parse_host(path, file_path, baudrate, "/dev/ttyACM0", baud); url_parse_query(query); return boost::make_shared<ConnSerial>(file_path, baudrate); }
avs_url_t *avs_url_parse(const char *raw_url) { // In data, we store all the components from raw_url; // The input url, in its fullest possible form, looks like this: // // proto://user:password@hostname:port/path\0 // // The output data will look like this: // // proto\0user\0password\0hostname\0port\0/path\0 // // A copy of the original string would require strlen(raw_url)+1 bytes. // Then: // - we replace "://" with a single nullbyte : -2 bytes // - we replace ":" before password with nullbyte : +-0 bytes // - we replace "@" before hostname with nullbyte : +-0 bytes // - we replace ":" before port with nullbyte : +-0 bytes // - we add a nullbyte before path's "/" : +1 byte // - we add a "/" in path if it's empty or query string only: +1 byte // ----------- // TOTAL DIFFERENCE IN REQUIRED SIZE: 0 bytes // // Thus, we know that we need out->data to be strlen(raw_url)+1 bytes long. size_t data_length = strlen(raw_url) + 1; avs_url_t *out = (avs_url_t *) avs_calloc(1, offsetof(avs_url_t, data) + data_length); if (!out) { LOG(ERROR, "out of memory"); return NULL; } size_t data_out_ptr = 0; if (url_parse_protocol(&raw_url, &data_out_ptr, data_length, out) || url_parse_credentials(&raw_url, &data_out_ptr, data_length, out) || url_parse_host(&raw_url, &data_out_ptr, data_length, out) || url_parse_port(&raw_url, &data_out_ptr, data_length, out) || url_parse_path(&raw_url, &data_out_ptr, data_length, out) || url_parsed(raw_url)) { avs_free(out); return NULL; } return out; }
void* url_parse(const char* url) { size_t len; url_t* uri; const char *p; uri = (url_t*)url_new(); if(!uri) return NULL; len = strlen(url); p = strchr(url, '/'); if(!p) { url_parse_host(url, len, uri); uri->path = strdup("/"); // default to root } else { // scheme if(p>url && *(p-1)==':' && p[1]=='/' ) { uri->scheme = strndup(url, p-1-url); len -= p + 2 - url; url = p + 2; p = strchr(url, '/'); } // host and port if(!p) { url_parse_host(url, len, uri); uri->path = strdup("/"); // default to root } else { // "/web/index.html" if(p > url) { url_parse_host(url, p-url, uri); url = p; } // path p = strchr(url, '?'); if(!p) { uri->path = strdup(url); } else { uri->path = strndup(url, p-url); // param url_parse_param(p+1, uri); } } } return uri; }