← Back to team overview

sslug-teknik team mailing list archive

Re: Hvordan kan man "smage" på en URL (hente første 256 bytes)

 

Hans Christian Studt wrote:
Hej,

Mit problem er at nogle URL'er referere til nogle PDF filer som jeg ikke umiddelbart er interesseret i at hente, så jeg kunne godt tænke mig blot at smage på URL'ens indhold. Blot tilstrækkeligt til at "file" kan bestemme data typen.

Er der en måde at begrænse længden af det der hentes til f.eks 256 bytes, for så at afgøre om man virkelig er interesseret i URL'en ?

------ http-head.c --------- fra www-linuxsocket.org ------------
/* http-client.c
 *
 * Copyright (c) 2000 Sean Walton and Macmillan Publishers.  Use may be in
 * whole or in part in accordance to the General Public License (GPL).
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
*/

/*****************************************************************************/
/*** http-client.c ***/ /*** ***/ /*** This program shows what the HTTP server sends to the client. First, ***/ /*** it opens a TCP socket to the server. Then, it sends the request ***/ /*** "GET <resource> HTTP/1.0\n\n" (the second newline is needed for the ***/ /*** "message-end" message. Lastly it prints out the reply. ***/
/*****************************************************************************/

#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>

#define MAXBUF  1024
void PANIC(char *msg);
#define PANIC(msg)  {perror(msg); abort();}

void TRACE(char *msg);
#define TRACE(msg)  {printf("%s\n",msg);}

int main(int Count, char *Strings[])
{   int sockfd, bytes_read;
    struct sockaddr_in dest;
    char buffer[MAXBUF];
    int Rc;
    char ErrTxt[256];

TRACE("# 20000");
    /*---Make sure we have the right number of parameters---*/
    if ( Count != 3 )
        {
        PANIC("usage: testport <IP-addr> <send-msg>\n");
        }
TRACE("# 21000");
sprintf(ErrTxt, "# 20100 Strings[1] = '%s'", Strings[1]);
TRACE(ErrTxt);
sprintf(ErrTxt, "# 20200 Strings[2] = '%s'", Strings[2]);
TRACE(ErrTxt);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
TRACE("# 21200");
    if ( sockfd < 0 )
        {
        PANIC("Socket");
        }
TRACE("# 21300");

    /*---Initialize server address/port struct---*/
    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(80); /*default HTTP Server port */
    Rc = inet_addr(Strings[1], &dest.sin_addr.s_addr);
TRACE("# 21400");
    if ( Rc == 0 )
        {
        PANIC(Strings[1]);
        }
TRACE("# 21500");

    /*---Connect to server---*/
TRACE("# 21600");
    Rc = connect(sockfd, (struct sockaddr*)&dest, sizeof(dest));
TRACE("# 21700");
    if ( Rc != 0 )
        {
        sprintf(ErrTxt, "Connect error code '%d'", Rc );
        PANIC(ErrTxt);
        }
TRACE("# 21800");

    sprintf(buffer, "HEAD %s HTTP/1.0\n\n", Strings[2]);
TRACE("# 21900");
    send(sockfd, buffer, strlen(buffer), 0);
TRACE("# 22000");

    /*---While there's data, read and print it---*/
    do
        {
        bzero(buffer, sizeof(buffer));
        bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
        if ( bytes_read > 0 )
            {
            printf("%s", buffer);
            }
        }
    while ( bytes_read > 0 );

    /*---Clean up---*/
    close(sockfd);
    return 0;
}
-------------------------------------------

-------------------------------------------
(/user/w3c/http) #./helsinge-2.sh
++ ./http-head 193.88.113.181 '/nethotel-include/WoBilag.dll?InfoId=1271474&amp;BilagId=116022'
# 20000
# 21000
# 20100 Strings[1] = '193.88.113.181'
# 20200 Strings[2] = '/nethotel-include/WoBilag.dll?InfoId=1271474&amp;BilagId=116022'
# 21200
# 21300
# 21400
# 21500
# 21600
# 21700
Connect error code '-1': Connection refused
./helsinge-2.sh: line 2: 25879 Afbrudt (SIGABRT) (core dumped) ./http-head 193.88.113.181 '/nethotel-include/WoBilag.dll?InfoId=1271474&amp;BilagId=116022'
-------------------------------------------

Hvad mon der går galt ?
--
Hans Christian Studt  mailto:hc@xxxxxxxx  http://hc.studt.dk
Free Software is the carrot. Microsoft is the stick.
Linux information
http://linux.studt.dk
.dk og W3C : http://home13.inet.tele.dk/hcstudt/w3c/index.html



References