int conn_setup( conn_t *conn ) { if( conn->ftp->fd <= 0 && conn->http->fd <= 0 ) if( !conn_init( conn ) ) return( 0 ); if( conn->proto == PROTO_FTP && !conn->proxy ) { if( !ftp_data( conn->ftp ) ) /* Set up data connnection */ return( 0 ); conn->fd = conn->ftp->data_fd; if( conn->currentbyte ) { ftp_command( conn->ftp, "REST %lld", conn->currentbyte ); if( ftp_wait( conn->ftp ) / 100 != 3 && conn->ftp->status / 100 != 2 ) return( 0 ); } } else { char s[MAX_STRING]; int i; snprintf( s, MAX_STRING, "%s%s", conn->dir, conn->file ); conn->http->firstbyte = conn->currentbyte; conn->http->lastbyte = conn->lastbyte; http_get( conn->http, s ); http_addheader( conn->http, "User-Agent: %s", conn->conf->user_agent ); for( i = 0; i < conn->conf->add_header_count; i++) http_addheader( conn->http, "%s", conn->conf->add_header[i] ); } return( 1 ); }
void http_command(struct http_channel *c) { const char *command = http_argbyname(c->request, "command"); struct http_response *rs = http_create_response(c); int i; c->response = rs; http_addheader(rs, "Expires", "Thu, 19 Nov 1981 08:52:00 GMT"); http_addheader(rs, "Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); if (!command) { error(rs, PAZPAR2_MISSING_PARAMETER, "command"); return; } for (i = 0; commands[i].name; i++) if (!strcmp(commands[i].name, command)) { (*commands[i].fun)(c); break; } if (!commands[i].name) error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "command"); return; }
int conn_setup(conn_t *conn) { #if WIN32 if (INVALID_SOCKET == conn->ftp->fd && INVALID_SOCKET == conn->http->fd) #else if (conn->ftp->fd <= 0 && conn->http->fd <= 0) #endif { if (!conn_init(conn)) { return 0; } } if (conn->proto == PROTO_FTP && !conn->proxy) { /* Set up data connnection */ if(!ftp_data(conn->ftp)) { return 0; } conn->fd = conn->ftp->data_fd; if (conn->currentbyte) { ftp_command(conn->ftp, "REST %lld", conn->currentbyte); if (ftp_wait(conn->ftp) / 100 != 3 && conn->ftp->status / 100 != 2) { return 0; } } } else { char s[MAX_STRING]; int i; snprintf(s, MAX_STRING, "%s%s", conn->dir, conn->file); conn->http->firstbyte = conn->currentbyte; conn->http->lastbyte = conn->lastbyte; http_get(conn->http, s); http_addheader(conn->http, "User-Agent: %s", conn->conf->user_agent); for (i = 0; i < conn->conf->add_header_count; i++) { http_addheader(conn->http, "%s", conn->conf->add_header[i]); } } return 1; }
void http_get( http_t *conn, char *lurl ) { *conn->request = 0; if( conn->proxy ) { http_addheader( conn, "GET %s://%s%s HTTP/1.0", conn->proto == PROTO_HTTP ? "http" : "ftp", conn->host, lurl ); } else { http_addheader( conn, "GET %s HTTP/1.0", lurl ); http_addheader( conn, "Host: %s", conn->host ); } if( *conn->auth ) http_addheader( conn, "Authorization: Basic %s", conn->auth ); if( *conn->proxy_auth ) { http_addheader( conn, "Proxy-Authorization: Basic %s", conn->proxy_auth ); } if( conn->firstbyte ) { if( conn->lastbyte ) http_addheader( conn, "Range: bytes=%lld-%lld", conn->firstbyte, conn->lastbyte ); else http_addheader( conn, "Range: bytes=%lld-", conn->firstbyte ); } }
int http_exec( http_t *conn ) { int i = 0; char s[2] = " ", *s2; #ifdef DEBUG fprintf( stderr, "--- Sending request ---\n%s--- End of request ---\n", conn->request ); #endif http_addheader( conn, "" ); write( conn->fd, conn->request, strlen( conn->request ) ); *conn->headers = 0; /* Read the headers byte by byte to make sure we don't touch the actual data */ while( 1 ) { if( read( conn->fd, s, 1 ) <= 0 ) { /* We'll put the message in conn->headers, not in request */ sprintf( conn->headers, _("Connection gone.\n") ); return( 0 ); } if( *s == '\r' ) { continue; } else if( *s == '\n' ) { if( i == 0 ) break; i = 0; } else { i ++; } strncat( conn->headers, s, MAX_QUERY ); } #ifdef DEBUG fprintf( stderr, "--- Reply headers ---\n%s--- End of headers ---\n", conn->headers ); #endif sscanf( conn->headers, "%*s %3i", &conn->status ); s2 = strchr( conn->headers, '\n' ); *s2 = 0; strcpy( conn->request, conn->headers ); *s2 = '\n'; return( 1 ); }