2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
22 #include "happyhttp.h"
23 using namespace happyhttp
;
27 static void http_begin_cb( const Response
* r
, void* userdata
)
29 switch ( r
->getstatus() ) {
31 ((Http
*)userdata
)->m_size
= 0;
34 //fprintf(stderr,"http status=%d %s\n",r->getstatus(),r->getreason());
35 ((Http
*)userdata
)->m_err
= r
->getreason();
36 ((Http
*)userdata
)->m_size
= -1;
41 static void http_get_cb( const Response
* r
, void* userdata
,
42 const unsigned char* data
, int numbytes
)
44 fwrite( data
, 1, numbytes
, ((Http
*)userdata
)->m_file
);
45 ((Http
*)userdata
)->m_size
+= numbytes
;
48 static void http_post_cb( const Response
* r
, void* userdata
,
49 const unsigned char* data
, int numbytes
)
51 //printf("received %d bytes [%s]\n",numbytes,data);
54 static void http_complete_cb( const Response
* r
, void* userdata
)
56 if ( r
->getreason() ) {
57 ((Http
*)userdata
)->m_err
= r
->getreason();
59 if ( r
->getheader("NP-Upload-Id") ) {
60 ((Http
*)userdata
)->m_npid
= r
->getheader("NP-Upload-Id");
65 static bool parseUri( const char * uri
,
70 if ( strncmp(uri
,"http://",strlen("http://"))==0 ) {
71 uri
+= strlen("http://");
74 char* e
= strchr(outHost
,'/');
80 e
= strchr(outHost
,':');
85 strcpy( outPath
, strchr(uri
,'/') );
86 //fprintf(stderr,"Http::get host=%s port=%d file=%s\n",
87 // outHost,*outPort,outPath);
91 bool Http::get( const char* uri
,
98 m_file
= fopen( file
, "wt" );
101 if ( parseUri( uri
, &host
[0], &port
, &path
[0] )
102 && path
[0] && host
[0] ) {
104 Connection
con( host
, port
);
105 con
.setcallbacks( http_begin_cb
, http_get_cb
, http_complete_cb
, this );
106 con
.request("GET",path
,NULL
,NULL
,0);
107 while ( con
.outstanding() ) {
108 //fprintf(stderr,"http_get pump\n");
111 } catch ( Wobbly w
) {
112 fprintf(stderr
,"http_get wobbly: %s\n",w
.what());
122 bool Http::post( const char* uri
, const char*putname
, const char* putfile
,
123 const char* otherargs
)
131 sprintf(data
,"%s&%s=",otherargs
,putname
);
133 sprintf(data
,"%s=",putname
);
135 char *buf
= &data
[strlen(data
)];
137 m_file
= fopen( putfile
, "rt" );
138 while ( !feof(m_file
) ) {
139 unsigned char c
= fgetc( m_file
);
152 //m_size = fread(data,1,sizeof(data),m_file);
155 m_size
= buf
- &data
[0];
157 const char* headers
[] = {
158 "Connection", "close",
159 "Content-type", "application/x-www-form-urlencoded",
160 "Accept", "text/plain",
164 if ( parseUri( uri
, &host
[0], &port
, &path
[0] ) ) {
166 Connection
con( host
, port
);
167 con
.setcallbacks( http_begin_cb
, http_post_cb
, http_complete_cb
, this );
168 con
.request("POST",path
,headers
,(unsigned char*)data
,m_size
);
169 while ( con
.outstanding() ) {
170 //fprintf(stderr,"http::post pump\n");
173 } catch ( Wobbly w
) {
174 fprintf(stderr
,"http_get wobbly: %s\n",w
.what());
181 std::string
Http::errorMessage()
186 std::string
Http::getHeader( const char* name
)