Fossil

Diff
Login

Differences From Artifact [6002b9785d]:

To Artifact [4dcbc27ce8]:


29
30
31
32
33
34
35
36
37
38
39
40








41
42
43
44
45
46
47
29
30
31
32
33
34
35





36
37
38
39
40
41
42
43
44
45
46
47
48
49
50







-
-
-
-
-
+
+
+
+
+
+
+
+







#include "config.h"
#include "http_transport.h"

/*
** State information
*/
static struct {
  int isOpen;     /* True when the transport layer is open */
  char *pBuf;     /* Buffer used to hold the reply */
  int nAlloc;     /* Space allocated for transportBuf[] */
  int nUsed ;     /* Space of transportBuf[] used */
  int iCursor;    /* Next unread by in transportBuf[] */
  int isOpen;             /* True when the transport layer is open */
  char *pBuf;             /* Buffer used to hold the reply */
  int nAlloc;             /* Space allocated for transportBuf[] */
  int nUsed ;             /* Space of transportBuf[] used */
  int iCursor;            /* Next unread by in transportBuf[] */
  FILE *pFile;            /* File I/O for FILE: */
  char *zOutFile;         /* Name of outbound file for FILE: */
  char *zInFile;          /* Name of inbound file for FILE: */
} transport = {
  0, 0, 0, 0, 0
};

/*
** Return the current transport error message.
*/
62
63
64
65
66
67
68
69
70











71
72
73
74
75
76
77
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







-
-
+
+
+
+
+
+
+
+
+
+
+







int transport_open(void){
  int rc = 0;
  if( transport.isOpen==0 ){
    if( g.urlIsHttps ){
      socket_set_errmsg("HTTPS: is not yet implemented");
      rc = 1;
    }else if( g.urlIsFile ){
      socket_set_errmsg("FILE: is not yet implemented");
      rc = 1;
      sqlite3_uint64 iRandId;
      sqlite3_randomness(sizeof(iRandId), &iRandId);
      transport.zOutFile = mprintf("%s-%llu-out.http", 
                                       g.zRepositoryName, iRandId);
      transport.zInFile = mprintf("%s-%llu-in.http", 
                                       g.zRepositoryName, iRandId);
      transport.pFile = fopen(transport.zOutFile, "wb");
      if( transport.pFile==0 ){
        fossil_fatal("cannot output temporary file: %s", transport.zOutFile);
      }
      transport.isOpen = 1;
    }else{
      rc = socket_open();
      if( rc==0 ) transport.isOpen = 1;
    }
  }
  return rc;
}
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
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
156
157
158
159







-
+
+
+
+
+
+
+
+











+
+



-
+

-
-















-
+
+
+
+
+
+
+
+







    transport.pBuf = 0;
    transport.nAlloc = 0;
    transport.nUsed = 0;
    transport.iCursor = 0;
    if( g.urlIsHttps ){
      /* TBD */
    }else if( g.urlIsFile ){
      /* TBD */
      if( transport.pFile ){ 
        fclose(transport.pFile);
        transport.pFile = 0;
      }
      unlink(transport.zInFile);
      free(transport.zInFile);
      unlink(transport.zOutFile);
      free(transport.zOutFile);
    }else{
      socket_close();
    }
    transport.isOpen = 0;
  }
}

/*
** Send content over the wire.
*/
void transport_send(Blob *toSend){
  char *z = blob_buffer(toSend);
  int n = blob_size(toSend);
  if( g.urlIsHttps ){
    /* TBD */
  }else if( g.urlIsFile ){
    /* TBD */
    fwrite(z, 1, n, transport.pFile);
  }else{
    char *z = blob_buffer(toSend);
    int n = blob_size(toSend);
    int sent;
    while( n>0 ){
      sent = socket_send(0, z, n);
      if( sent<=0 ) break;
      n -= sent;
    }
  }
}

/*
** This routine is called when the outbound message is complete and
** it is time to being recieving a reply.
*/
void transport_flip(void){
  if( g.urlIsFile ){
    /* run "fossil http" to process the outbound message */
    char *zCmd;
    fclose(transport.pFile);
    zCmd = mprintf("\"%s\" http \"%s\" \"%s\" \"%s\" 127.0.0.1",
       g.argv[0], g.zRepositoryName, transport.zOutFile, transport.zInFile
    );
    system(zCmd);
    free(zCmd);
    transport.pFile = fopen(transport.zInFile, "rb");
  }
}

/*
** This routine is called when the inbound message has been received
** and it is time to start sending again.
*/
161
162
163
164
165
166
167
168
169

170
171
172
173
174
175
176
187
188
189
190
191
192
193


194
195
196
197
198
199
200
201







-
-
+







  }
  if( N>0 ){
    int got;
    if( g.urlIsHttps ){
      /* TBD */
      got = 0;
    }else if( g.urlIsFile ){
      /* TBD */
      got = 0;
      got = fread(zBuf, 0, N, transport.pFile);
    }else{
      got = socket_receive(0, zBuf, N);
    }
    if( got>0 ){
      nByte += got; 
    }
  }