summaryrefslogtreecommitdiffstats
path: root/nanohttp/nanohttp-socket.h
blob: 901876ff17df899f193ff483fb6683612f63be0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/******************************************************************
 *  $Id: nanohttp-socket.h,v 1.2 2003/12/16 13:16:14 snowdrop Exp $
 *
 * CSOAP Project:  A http client/server library in C
 * Copyright (C) 2003  Ferhat Ayaz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * 
 * Email: ayaz@jprogrammer.net
 ******************************************************************/
#ifndef NANO_HTTP_SOCKET_H 
#define NANO_HTTP_SOCKET_H 


#define HSOCKET_OK 0
#define HSOCKET_CAN_NOT_CREATE_SOCKET 1001
#define HSOCKET_CAN_NOT_GET_HOSTNAME 1002
#define HSOCKET_CAN_NOT_CONNECT 1003
#define HSOCKET_CAN_NOT_SEND 1004
#define HSOCKET_CAN_NOT_RECEIVE 1005

#define HSOCKET_MAX_BUFSIZE 1024

typedef int hsocket_t;

/*
  PROTOTYPE:
  int my_recv_cb(hsocket_t sock, char *buffer, int size, void *userdata);
  returns 1 to continue 0 to stop receiving.
 */
typedef int (*hsocket_recv_callback)(hsocket_t, char *, int, void*);


/*
  hsocket_module_init
  Returns 0 if success.
  >0 if fail.
 */
int hsocket_module_init();
void hsocket_module_destroy();


/*
  hsocket_init
  Returns 0 if success.
  >0 if fail.
 */
int hsocket_init(hsocket_t *sock);
void hsocket_free(hsocket_t sock);


/*
  hsocket_open
  Returns 0 if success
  >0 if fail.
 */
int hsocket_open(hsocket_t *sock, const char* host, int port);
void hsocket_close(hsocket_t sock);


/*
  hsocket_send
  sends strlen(buffer) bytes of data
  Returns 0 if success
  >0 if fail
 */
int hsocket_send(hsocket_t sock, const char* buffer); 


/*
  hsocket_recv
  receives everything quequed on the socket.
  Sets *buffer to the received buffer.
  Sets size to the received size.
  You must free the buffer manually with free().
  If buffer is non zero, this functions assumes that
  buffer is valid and just reallocates the given buffer.
  If buffer is zero (like in the following example),
  the buffer will be allocated first.

  Example:

   int size;
   char *buffer;
   hsocket_t sock;
   
   buffer = 0; 
   sock = ...

   if (!hsocket_recv(sock, &buffer, &size)) {
    printf("Received total: %d\n", size);
    printf("Received: '%s'\n", buffer);

    free(buffer);
   } else {
    printf("Error receiving data\n");
   }

  Returns 0 if success
  >0 if fail
 */
int hsocket_recv(hsocket_t sock, char** buffer, int *size); 

int hsocket_recv_limit(hsocket_t sock, char** buffer, 
		       const char* delim, char **rest, 
		       int *totalBuffer, int *totalRest);

/*
  returns 1 to continue, 0 to break;
 */
int hsocket_recv_cb(hsocket_t sock, 
		    hsocket_recv_callback cb, void *userdata);

int hsocket_read(hsocket_t sock, char* buffer, int total);


/* ======================================== */
/*  Buffered socket                         */
/* ======================================== */
typedef struct _bufsocket
{
  hsocket_t sock;
  char *buffer;
  int bufsize;
  int cur;
}hbufsocket_t;


int hbufsocket_read(hbufsocket_t *bufsock, char *buffer, int size);

#endif