http.c
上传用户:seven77cht
上传日期:2007-01-04
资源大小:486k
文件大小:4k
- /***************************************
- $Header: /home/amb/wwwoffle/RCS/http.c 1.19 1999/12/23 20:57:56 amb Exp $
- WWWOFFLE - World Wide Web Offline Explorer - Version 2.5d.
- Functions for getting URLs using HTTP.
- ******************/ /******************
- Written by Andrew M. Bishop
- This file Copyright 1997,98,99 Andrew M. Bishop
- It may be distributed under the GNU Public License, version 2, or
- any higher version. See section COPYING of the GNU Public license
- for conditions under which this file may be redistributed.
- ***************************************/
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "wwwoffle.h"
- #include "errors.h"
- #include "misc.h"
- #include "config.h"
- #include "sockets.h"
- #include "proto.h"
- /*+ Set to the name of the proxy if there is one. +*/
- static char *proxy=NULL;
- /*+ The file descriptor of the server. +*/
- static int server=-1;
- /*++++++++++++++++++++++++++++++++++++++
- Open a connection to get a URL using HTTP.
- char *HTTP_Open Returns NULL on success, a useful message on error.
- URL *Url The URL to open.
- ++++++++++++++++++++++++++++++++++++++*/
- char *HTTP_Open(URL *Url)
- {
- char *msg=NULL;
- char *colon;
- char *server_host=NULL;
- int server_port=Protocols[Protocol_HTTP].defport;
- proxy=WhichProxy(Url->proto,Url->host);
- if(IsLocalNetHost(Url->host))
- proxy=NULL;
- if(proxy)
- {
- server_host=(char*)malloc(strlen(proxy)+1);
- strcpy(server_host,proxy);
- }
- else
- {
- server_host=(char*)malloc(strlen(Url->host)+1);
- strcpy(server_host,Url->host);
- }
- if((colon=strchr(server_host,':')))
- {
- *colon++=0;
- if(*colon)
- server_port=atoi(colon);
- }
- /* Open the connection. */
- server=OpenClientSocket(server_host,server_port,ConnectTimeout);
- init_buffer(server);
- if(server==-1)
- msg=PrintMessage(Warning,"Cannot open the HTTP connection to %s port %d; [%!s].",server_host,server_port);
- free(server_host);
- return(msg);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Write to the server to request the URL.
- char *HTTP_Request Returns NULL on success, a useful message on error.
- URL *Url The URL to get.
- Header *request_head The head of the HTTP request for the URL.
- Body *request_body The body of the HTTP request for the URL.
- ++++++++++++++++++++++++++++++++++++++*/
- char *HTTP_Request(URL *Url,Header *request_head,Body *request_body)
- {
- char *msg=NULL,*head;
- if(proxy)
- MakeRequestAuthorised(proxy,request_head);
- else
- MakeRequestNonProxy(request_head);
- head=HeaderString(request_head);
- if(proxy)
- PrintMessage(ExtraDebug,"Outgoing Request Head (to proxy)n%s",head);
- else
- PrintMessage(ExtraDebug,"Outgoing Request Head (to server)n%s",head);
- if(write_string(server,head)==-1)
- msg=PrintMessage(Warning,"Failed to write to remote HTTP %s [%!s].",proxy?"proxy":"server");
- if(request_body)
- {
- #if 0
- if(request_body->content[request_body->length-1]!='n')
- {
- request_body->content[request_body->length++]='r';
- request_body->content[request_body->length++]='n';
- }
- #endif
- if(write_data(server,request_body->content,request_body->length)==-1)
- msg=PrintMessage(Warning,"Failed to write to remote HTTP %s [%!s].",proxy?"proxy":"server");
- }
- free(head);
- return(msg);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Read a line from the header of the reply for the URL.
- char *HTTP_ReadHead Returns the next line of data, NULL on EOF.
- char *line The previous line read.
- ++++++++++++++++++++++++++++++++++++++*/
- char *HTTP_ReadHead(char *line)
- {
- return(read_line_or_timeout(server,line,SocketTimeout));
- }
- /*++++++++++++++++++++++++++++++++++++++
- Read bytes from the body of the reply for the URL.
- int HTTP_ReadBody Returns the number of bytes read on success, -1 on error.
- char *s A string to fill in with the information.
- int n The number of bytes to read.
- ++++++++++++++++++++++++++++++++++++++*/
- int HTTP_ReadBody(char *s,int n)
- {
- return(read_data_or_timeout(server,s,n,SocketTimeout));
- }
- /*++++++++++++++++++++++++++++++++++++++
- Close a connection opened using HTTP.
- int HTTP_Close Return 0 on success, -1 on error.
- ++++++++++++++++++++++++++++++++++++++*/
- int HTTP_Close(void)
- {
- return(CloseSocket(server));
- }