Add pointlog2svg utility for the thesis
[numtypysics.git] / Http.cpp
blob94c7a10804a17a50a444b9944274244ecca540e3
1 /*
2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
4 *
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.
9 *
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.
17 #include <cstdlib>
18 #include <stdio.h>
19 #include <string.h>
21 #include "Http.h"
22 #include "happyhttp.h"
23 using namespace happyhttp;
27 static void http_begin_cb( const Response* r, void* userdata )
29 switch ( r->getstatus() ) {
30 case OK:
31 ((Http*)userdata)->m_size = 0;
32 break;
33 default:
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;
37 break;
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,
66 char* outHost,
67 int * outPort,
68 char* outPath )
70 if ( strncmp(uri,"http://",strlen("http://"))==0 ) {
71 uri += strlen("http://");
73 strcpy(outHost,uri);
74 char* e = strchr(outHost,'/');
75 *outPort = 80;
77 if ( e ) {
78 *e = '\0';
80 e = strchr(outHost,':');
81 if ( e ) {
82 *e = '\0';
83 *outPort=atoi(e+1);
85 strcpy( outPath, strchr(uri,'/') );
86 //fprintf(stderr,"Http::get host=%s port=%d file=%s\n",
87 // outHost,*outPort,outPath);
88 return true;
91 bool Http::get( const char* uri,
92 const char* file )
94 char host[256];
95 char path[256];
96 int port;
98 m_file = fopen( file, "wt" );
99 m_size = -1;
101 if ( parseUri( uri, &host[0], &port, &path[0] )
102 && path[0] && host[0] ) {
103 try {
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");
109 con.pump();
111 } catch ( Wobbly w ) {
112 fprintf(stderr,"http_get wobbly: %s\n",w.what());
116 fclose ( m_file );
117 free( host );
118 return m_size > 0;
122 bool Http::post( const char* uri, const char*putname, const char* putfile,
123 const char* otherargs )
125 char host[256];
126 char path[256];
127 char data[64*1024];
128 int port;
130 if ( otherargs ) {
131 sprintf(data,"%s&%s=",otherargs,putname);
132 } else {
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 );
140 switch ( c ) {
141 case 'a'...'z':
142 case 'A'...'Z':
143 case '0'...'9':
144 *buf++ = c;
145 break;
146 default:
147 *buf++ = '%';
148 *buf++ = c>>8;
149 *buf++ = c&0xf;
150 break;
152 //m_size = fread(data,1,sizeof(data),m_file);
154 fclose ( 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] ) ) {
165 try {
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");
171 con.pump();
173 } catch ( Wobbly w ) {
174 fprintf(stderr,"http_get wobbly: %s\n",w.what());
180 // response
181 std::string Http::errorMessage()
183 return m_err;
186 std::string Http::getHeader( const char* name )
188 return m_npid;