void deleteObject(char * request, char * token, char * url, char * container, char * object) { char host[256], tail[256]; splitUrl(url,host,tail); sprintf(request, swt_ops_format[SWT_OBJECT_DELETE], tail, container, object, host, token); }
void listObjects(char * request, char * token, char * url, char * container) { char host[256], tail[256]; splitUrl(url,host,tail); sprintf(request, swt_ops_format[SWT_OBJECT_LIST], tail, container, host, token); }
void createObject(char * request, char * token, char * url, char * container, char * object, void * data, long size) { char host[256], tail[256]; splitUrl(url,host,tail); sprintf(request, swt_ops_format[SWT_OBJECT_CREATE], tail, container, object, host, token, size, data); }
void deleteContainer(char * request, char * token, char * url, char * name) { char host[256], tail[256]; splitUrl(url, host,tail); sprintf(request, swt_ops_format[SWT_CONTAINER_DELETE], tail, name, host, token); }
void listContainers(char * request, char * token, char * url) { char host[256], tail[256]; splitUrl(url,host,tail); sprintf(request, swt_ops_format[SWT_CONTAINER_LIST], tail, host, token); }
/***************************************************************************** * Main loop ***/ int main() { Turl *newUrl; newUrl=splitUrl("gopher://localhost/"); printf("method: %s\n", newUrl->method); printf("host : %s\n", newUrl->host); printf("direct: %s\n", newUrl->directory); printf("port : %d\n", newUrl->port); }
int main(void) { char theGood[] = "http://example.com/authv1/PikkuJose"; char theBad[] = "http://example.com//jander.com"; char theUgly[] = "s/berebere/tururu/pararapaparaduda/"; char host[50]; char tail[50]; printf("Host: %s\nTail: %s\n", host, tail); splitUrl(theGood,host, tail); printf("Host: %s\nTail: %s\n", host, tail); splitUrl(theBad,host, tail); printf("Host: %s\nTail: %s\n", host, tail); splitUrl(theUgly,host, tail); printf("Host: %s\nTail: %s\n", host, tail); printf("TheGood: %s; TheBad: %s; TheUgly: %s\n", theGood,theBad,theUgly); return 0; }
HttpFile::HttpFile(const char* fileUrl) :IO::File(), chunked(false),haveEof(false), fixedSize(false), unreadSize(0), gzipped(false) { /* Parse the URL to determine server name, port, and absolute resource location: */ URLParts urlParts=splitUrl(fileUrl); /* Connect to the HTTP server: */ pipe=new Comm::TCPPipe(urlParts.serverName.c_str(),urlParts.portNumber); /* Initialize the HTTP parser: */ init(urlParts); }
std::string shark::download(std::string const& url, unsigned short port){ // split the URL into domain and resource std::string domain, resource; std::tie(domain, resource) = splitUrl(url); // open a TCP/IP socket connection Socket socket(domain, port); if(!socket.connected()){ throw std::runtime_error("[download] can not connect to url"); } std::string request = "GET " + resource + " HTTP/1.1\r\nhost: " + domain + "\r\n\r\n"; socket.writeAll(request.c_str(), request.size()); // http reply data std::map<std::string, std::string> headers; std::string body; // parse http reply line std::string reply = socket.readLine(); if (reply.size() < 12) throw std::runtime_error("[download] http protocol violation"); if (reply.substr(0, 9) != "HTTP/1.0 " && reply.substr(0, 9) != "HTTP/1.1 ") throw std::runtime_error("[download] http protocol violation"); if (reply.substr(9, 3) != "200") throw std::runtime_error("[download] failed with HTTP status " + reply.substr(9)); // parse http headers while (true) { std::string h = socket.readLine(); if (h.empty()) break; std::size_t colon = h.find(":"); if (colon == std::string::npos) throw std::runtime_error("[download] http protocol violation"); std::string tag = h.substr(0, colon); // convert plain ASCII to lower case for (std::size_t i=0; i<tag.size(); i++) tag[i] = std::tolower(tag[i]); std::string value = h.substr(colon + 1); while (! value.empty() && value[0] == ' ') value.erase(0, 1); while (! value.empty() && value[value.size() - 1] == ' ') value.erase(value.size() - 1); // convert plain ASCII to lower case for (std::size_t i=0; i<value.size(); i++) value[i] = std::tolower(value[i]); headers[tag] = value; } // receive http body std::string len = headers["content-length"]; if (! len.empty()) { // a priori known content length std::size_t length = strtol(len.c_str(), NULL, 10); body = socket.readChunk(length); } else { // chunked encoding if (headers["transfer-encoding"] != "chunked") throw std::runtime_error("[download] transfer encoding not supported"); while (true) { std::string len = socket.readLine(); std::size_t length = strtol(len.c_str(), NULL, 16); body += socket.readChunk(length); std::string x = socket.readLine(); if (! x.empty()) throw std::runtime_error("[download] http protocol violation"); if (length == 0) break; } } return body; }
void ConfigEntry::setURLValueList( const KUrl::List& urls ) { QStringList lst; Q_FOREACH( const KUrl& i, urls ) { lst << splitUrl( m_argType, i ); }
void ConfigEntry::setURLValue( const KUrl& url ) { QString str = splitUrl( m_argType, url ); m_value = str; m_dirty = true; }
int eServiceTS::openHttpConnection(std::string url) { UrlComponents comp; if (splitUrl(url, comp) != 0) { eDebug("[VLC] invalid url: %s", url.c_str()); return -1; } struct hostent* h = gethostbyname(comp.host.c_str()); if (h == NULL || h->h_addr_list == NULL) return -1; int fd = socket(PF_INET, SOCK_STREAM, 0); if (fd == -1) return -1; struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = *((in_addr_t*)h->h_addr_list[0]); addr.sin_port = htons(comp.port); eDebug("[VLC] connecting to %s", url.c_str()); if (connect(fd, (sockaddr*)&addr, sizeof(addr)) == -1) { std::string msg = "connect failed for: " + url; eDebug(msg.c_str()); return -1; } std::string request = "GET "; request.append(comp.uri).append(" HTTP/1.1\n"); request.append("Accept: */*\n"); if (comp.authorization.length() > 0) { request.append("Authorization: Basic ").append(comp.authorization).append("\n"); } request.append("Host: ").append(comp.host); if (comp.port != 80) { char buf[20]; sprintf(buf, ":%d", comp.port); request.append(buf); } request.append("\n"); request.append("Connection: close\n"); request.append("\n"); //eDebug(request.c_str()); write(fd, request.c_str(), request.length()); int rc; size_t buflen = 1000; char* linebuf = (char*)malloc(1000); rc = getline(&linebuf, &buflen, fd); //eDebug("[VLC] RECV(%d): %s", rc, linebuf); if (rc <= 0) { close(fd); free(linebuf); return -1; } char proto[100]; int statuscode = 0; char statusmsg[100]; rc = sscanf(linebuf, "%99s %d %99s", proto, &statuscode, statusmsg); if (rc != 3 || statuscode != 200) { eDebug(request.c_str()); eDebug("[VLC] wrong response %d: \"200 OK\" expected.", statuscode); free(linebuf); close(fd); return -1; } //eDebug("[VLC] %s %d %s", proto, statuscode, statusmsg); while (rc > 0) { rc = getline(&linebuf, &buflen, fd); //eDebug("[VLC] RECV(%d): %s", rc, linebuf); } free(linebuf); return fd; }