* Added wxsocket lib and sample (I hope I don't forget some file)
* Updated some wx data and makefiles * Updates on wxStream (reorganization) makefile for Windows will nearly follow wxSocket should work on wxGTK (I've tested it) * IPC over Network is included git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@684 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
251
src/common/http.cpp
Normal file
251
src/common/http.cpp
Normal file
@@ -0,0 +1,251 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: http.cpp
|
||||
// Purpose: HTTP protocol
|
||||
// Author: Guilhem Lavaux
|
||||
// Modified by:
|
||||
// Created: August 1997
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1997, 1998 Guilhem Lavaux
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "http.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "wx/string.h"
|
||||
#include "wx/tokenzr.h"
|
||||
#include "wx/socket.h"
|
||||
#include "wx/protocol/protocol.h"
|
||||
#include "wx/protocol/http.h"
|
||||
#include "wx/sckstrm.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#if !USE_SHARED_LIBRARY
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
|
||||
IMPLEMENT_PROTOCOL(wxHTTP, "http", "80", TRUE)
|
||||
#endif
|
||||
|
||||
#define HTTP_BSIZE 2048
|
||||
|
||||
wxHTTP::wxHTTP()
|
||||
: wxProtocol(),
|
||||
m_headers(wxKEY_STRING)
|
||||
{
|
||||
m_addr = NULL;
|
||||
m_read = FALSE;
|
||||
|
||||
SetNotify(REQ_LOST);
|
||||
}
|
||||
|
||||
wxHTTP::~wxHTTP()
|
||||
{
|
||||
// wxString isn't a wxObject
|
||||
wxNode *node = m_headers.First();
|
||||
wxString *string;
|
||||
|
||||
while (node) {
|
||||
string = (wxString *)node->Data();
|
||||
delete string;
|
||||
node = node->Next();
|
||||
}
|
||||
}
|
||||
|
||||
wxString wxHTTP::GetContentType()
|
||||
{
|
||||
return GetHeader("Content-Type");
|
||||
}
|
||||
|
||||
void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
|
||||
{
|
||||
if (m_read) {
|
||||
m_headers.Clear();
|
||||
m_read = FALSE;
|
||||
}
|
||||
|
||||
wxNode *node = m_headers.Find(header);
|
||||
|
||||
if (!node)
|
||||
m_headers.Append(header, (wxObject *)(new wxString(h_data)));
|
||||
else {
|
||||
wxString *str = (wxString *)node->Data();
|
||||
(*str) = h_data;
|
||||
}
|
||||
}
|
||||
|
||||
wxString wxHTTP::GetHeader(const wxString& header)
|
||||
{
|
||||
wxNode *node = m_headers.Find(header);
|
||||
if (!node)
|
||||
return (char *)NULL;
|
||||
|
||||
return *((wxString *)node->Data());
|
||||
}
|
||||
|
||||
void wxHTTP::SendHeaders()
|
||||
{
|
||||
wxNode *head = m_headers.First();
|
||||
|
||||
while (head) {
|
||||
wxString *str = (wxString *)head->Data();
|
||||
char buf[100];
|
||||
|
||||
sprintf(buf, "%s: %s\n\r", head->key.string, str->GetData());
|
||||
Write(buf, strlen(buf));
|
||||
|
||||
head = head->Next();
|
||||
}
|
||||
}
|
||||
|
||||
bool wxHTTP::ParseHeaders()
|
||||
{
|
||||
wxString line;
|
||||
|
||||
m_headers.Clear();
|
||||
m_read = TRUE;
|
||||
|
||||
while (1) {
|
||||
m_error = GetLine(this, line);
|
||||
if (m_error != wxPROTO_NOERR)
|
||||
return FALSE;
|
||||
|
||||
if (line.Length() == 0)
|
||||
break;
|
||||
|
||||
int pos = line.Find(':');
|
||||
if (pos == -1)
|
||||
return FALSE;
|
||||
|
||||
wxString left_str = line(0, pos);
|
||||
wxString right_str = line(pos+1, line.Length());
|
||||
|
||||
right_str = right_str.Strip(wxString::leading);
|
||||
|
||||
wxString *str = new wxString(right_str);
|
||||
|
||||
m_headers.Append(left_str, (wxObject *) str);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxHTTP::Connect(const wxString& host)
|
||||
{
|
||||
wxIPV4address *addr;
|
||||
|
||||
if (m_connected) {
|
||||
delete m_addr;
|
||||
m_addr = NULL;
|
||||
Close();
|
||||
}
|
||||
|
||||
m_addr = addr = new wxIPV4address();
|
||||
|
||||
if (!addr->Hostname(host)) {
|
||||
delete m_addr;
|
||||
m_addr = NULL;
|
||||
m_error = wxPROTO_NETERR;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!addr->Service("http"))
|
||||
addr->Service(80);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxHTTP::Connect(wxSockAddress& addr)
|
||||
{
|
||||
struct sockaddr *raw_addr;
|
||||
size_t len;
|
||||
|
||||
m_addr = (wxSockAddress *)(addr.GetClassInfo()->CreateObject());
|
||||
|
||||
addr.Build(raw_addr, len);
|
||||
m_addr->Disassemble(raw_addr, len);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
|
||||
{
|
||||
char *tmp_buf;
|
||||
char buf[HTTP_BSIZE];
|
||||
|
||||
switch (req) {
|
||||
case wxHTTP_GET:
|
||||
tmp_buf = "GET";
|
||||
break;
|
||||
default:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
sprintf(buf, "%s %s HTTP/1.0\n\r", tmp_buf, (const char *)path);
|
||||
Write(buf, strlen(buf));
|
||||
SendHeaders();
|
||||
sprintf(buf, "\n\r");
|
||||
Write(buf, strlen(buf));
|
||||
|
||||
wxString tmp_str;
|
||||
|
||||
m_error = GetLine(this, tmp_str);
|
||||
if (m_error != wxPROTO_NOERR)
|
||||
return FALSE;
|
||||
|
||||
if (!tmp_str.Contains("HTTP/")) {
|
||||
// TODO: support HTTP v0.9 which can have no header.
|
||||
SetHeader("Content-Length", "-1");
|
||||
SetHeader("Content-Type", "none/none");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
wxStringTokenizer token(tmp_str,' ');
|
||||
wxString tmp_str2;
|
||||
|
||||
token.NextToken();
|
||||
tmp_str2 = token.NextToken();
|
||||
|
||||
switch (atoi(tmp_str2)) {
|
||||
case 200:
|
||||
break;
|
||||
default:
|
||||
m_error = wxPROTO_NOFILE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return ParseHeaders();
|
||||
}
|
||||
|
||||
class wxHTTPStream : public wxSocketInputStream {
|
||||
public:
|
||||
wxHTTP *m_http;
|
||||
wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
|
||||
virtual ~wxHTTPStream(void) { m_http->Abort(); }
|
||||
};
|
||||
|
||||
bool wxHTTP::Abort(void)
|
||||
{
|
||||
return wxSocketClient::Close();
|
||||
}
|
||||
|
||||
wxInputStream *wxHTTP::GetInputStream(const wxString& path)
|
||||
{
|
||||
wxHTTPStream *inp_stream = new wxHTTPStream(this);
|
||||
|
||||
if (!m_addr || m_connected) {
|
||||
m_error = wxPROTO_CONNERR;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!wxProtocol::Connect(*m_addr))
|
||||
return NULL;
|
||||
|
||||
if (!BuildRequest(path, wxHTTP_GET))
|
||||
return NULL;
|
||||
|
||||
return inp_stream;
|
||||
}
|
Reference in New Issue
Block a user