void CDropbox::HandleHttpResponseError(NETLIBHTTPREQUEST *response) { if (response == NULL) throw TransferException(HttpStatusToText(HTTP_STATUS_ERROR)); if (response->resultCode != HTTP_STATUS_OK) throw TransferException(HttpStatusToText((HTTP_STATUS)response->resultCode)); }
void Web::Request::Connect() { SOCKET httpSocket; int ret; struct sockaddr_in httpAddr; struct hostent *hostinfo; char buffer[REQUESTBUFFERSIZE + 1]; int bufferSize; int timeout = REQUESTTIMEOUT; /* Choses settings for the socket to connect to */ httpAddr.sin_family = AF_INET; /* Set the port */ //httpAddr.sin_port = htons(url->getPort()); httpAddr.sin_port = htons(80); /* Do a hostname lookup on the host */ //hostinfo = gethostbyname (url->getHost()); hostinfo = gethostbyname ("localhost"); if (hostinfo == NULL) { throw HostNotFoundException(); } /* Fill the httpAddr with IP data */ httpAddr.sin_addr = *(struct in_addr *) hostinfo->h_addr; /* Creates the socket */ httpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ; /* Check for errors */ if (httpSocket == INVALID_SOCKET) throw SocketException(); /* Add a timeout on all communiction*/ ret = setsockopt(httpSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); /* Check for errors */ if (ret == SOCKET_ERROR) { closesocket(httpSocket); throw SocketException(); } /* Connect to the server */ ret = connect(httpSocket,(LPSOCKADDR)&httpAddr, sizeof(struct sockaddr)); /* Check for errors */ if (ret == SOCKET_ERROR) { closesocket(httpSocket); throw ConnectionFailedException(WSAGetLastError()); } /* Create a request */ bufferSize = _snprintf(buffer, REQUESTBUFFERSIZE, "GET %s HTTP/1.0\r\n\r\n", url->getURL()); if (bufferSize <= 0) { closesocket(httpSocket); throw Exception("Request Buffer Too Small"); } /* Send a HTTP request */ ret = send(httpSocket, buffer, bufferSize, 0); if (ret < bufferSize) { closesocket(httpSocket); throw TransferException(); } reply.assign(""); ret = REQUESTBUFFERSIZE; /* Now wait for the return and loop while its coming */ while (ret > 0) { ret = recv(httpSocket, buffer, REQUESTBUFFERSIZE, 0); /* Append data to the buffer */ if (ret > 0) reply.append(buffer, ret); } /* Clean up the socket */ closesocket(httpSocket); /* Now if we didn't get any data throw a error */ if (reply.length()==0) { throw TransferException(); } /* Got some data let parse it */ ret = (int)reply.find("\r\n\r\n"); if (ret==-1) { throw InvalidDataException(); } header = reply.substr(0, ret); ret += 4; body = reply.substr(ret, reply.length() - ret); }