comparison libmpdemux/stream_ftp.c @ 16761:1eeb10c3e534

Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller) Patch by Zuxy Meng < zuxy POIS meng AH gmail POIS com > Original thread: Date: Oct 14, 2005 1:01 PM Subject: [MPlayer-dev-eng] [PATCH] More user friendly ftp error
author gpoirier
date Fri, 14 Oct 2005 12:35:30 +0000
parents 0a31740dd5e6
children 6ff3379a0862
comparison
equal deleted inserted replaced
16760:5ce7bc671362 16761:1eeb10c3e534
170 170
171 171
172 static int FtpSendCmd(const char *cmd, struct stream_priv_s *nControl,char* rsp) 172 static int FtpSendCmd(const char *cmd, struct stream_priv_s *nControl,char* rsp)
173 { 173 {
174 int l = strlen(cmd); 174 int l = strlen(cmd);
175 int hascrlf = cmd[l - 2] == '\r' && cmd[l - 1] == '\n';
175 176
176 mp_msg(MSGT_STREAM,MSGL_V, "[ftp] > %s",cmd); 177 mp_msg(MSGT_STREAM,MSGL_V, "[ftp] > %s",cmd);
177 while(l > 0) { 178 while(l > 0) {
178 int s = send(nControl->handle,cmd,l,0); 179 int s = send(nControl->handle,cmd,l,0);
179 180
184 185
185 cmd += s; 186 cmd += s;
186 l -= s; 187 l -= s;
187 } 188 }
188 189
189 return readresp(nControl,rsp); 190 if (hascrlf)
191 return readresp(nControl,rsp);
192 else
193 return FtpSendCmd("\r\n", nControl, rsp);
190 } 194 }
191 195
192 static int FtpOpenPort(struct stream_priv_s* p) { 196 static int FtpOpenPort(struct stream_priv_s* p) {
193 int resp,fd; 197 int resp,fd;
194 char rsp_txt[256]; 198 char rsp_txt[256];
195 char* par,str[128]; 199 char* par,str[128];
196 int num[6]; 200 int num[6];
197 201
198 resp = FtpSendCmd("PASV\r\n",p,rsp_txt); 202 resp = FtpSendCmd("PASV",p,rsp_txt);
199 if(resp != 2) { 203 if(resp != 2) {
200 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'PASV' failed: %s\n",rsp_txt); 204 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'PASV' failed: %s\n",rsp_txt);
201 return 0; 205 return 0;
202 } 206 }
203 207
296 s->eof = 1; 300 s->eof = 1;
297 return 0; 301 return 0;
298 } 302 }
299 // Send the ABOR command 303 // Send the ABOR command
300 // Ignore the return code as sometimes it fail with "nothing to abort" 304 // Ignore the return code as sometimes it fail with "nothing to abort"
301 FtpSendCmd("ABOR\r\n",p,rsp_txt); 305 FtpSendCmd("ABOR",p,rsp_txt);
302 } 306 }
303 307
304 // Open a new connection 308 // Open a new connection
305 s->fd = FtpOpenPort(p); 309 s->fd = FtpOpenPort(p);
306 310
307 if(!s->fd) return 0; 311 if(!s->fd) return 0;
308 312
309 if(newpos > 0) { 313 if(newpos > 0) {
310 snprintf(str,255,"REST %"PRId64"\r\n", (int64_t)newpos); 314 snprintf(str,255,"REST %"PRId64, (int64_t)newpos);
311 315
312 resp = FtpSendCmd(str,p,rsp_txt); 316 resp = FtpSendCmd(str,p,rsp_txt);
313 if(resp != 3) { 317 if(resp != 3) {
314 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 318 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
315 newpos = 0; 319 newpos = 0;
316 } 320 }
317 } 321 }
318 322
319 // Get the file 323 // Get the file
320 snprintf(str,255,"RETR %s\r\n",p->filename); 324 snprintf(str,255,"RETR %s",p->filename);
321 resp = FtpSendCmd(str,p,rsp_txt); 325 resp = FtpSendCmd(str,p,rsp_txt);
322 326
323 if(resp != 1) { 327 if(resp != 1) {
324 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 328 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
325 return 0; 329 return 0;
338 if(s->fd > 0) { 342 if(s->fd > 0) {
339 closesocket(s->fd); 343 closesocket(s->fd);
340 s->fd = 0; 344 s->fd = 0;
341 } 345 }
342 346
343 FtpSendCmd("QUIT\r\n",p,NULL); 347 FtpSendCmd("QUIT",p,NULL);
344 348
345 if(p->handle) closesocket(p->handle); 349 if(p->handle) closesocket(p->handle);
346 if(p->buf) free(p->buf); 350 if(p->buf) free(p->buf);
347 351
348 m_struct_free(&stream_opts,p); 352 m_struct_free(&stream_opts,p);
385 m_struct_free(&stream_opts,opts); 389 m_struct_free(&stream_opts,opts);
386 return STREAM_ERROR; 390 return STREAM_ERROR;
387 } 391 }
388 392
389 // Login 393 // Login
390 snprintf(str,255,"USER %s\r\n",p->user); 394 snprintf(str,255,"USER %s",p->user);
391 resp = FtpSendCmd(str,p,rsp_txt); 395 resp = FtpSendCmd(str,p,rsp_txt);
392 396
393 // password needed 397 // password needed
394 if(resp == 3) { 398 if(resp == 3) {
395 snprintf(str,255,"PASS %s\r\n",p->pass); 399 snprintf(str,255,"PASS %s",p->pass);
396 resp = FtpSendCmd(str,p,rsp_txt); 400 resp = FtpSendCmd(str,p,rsp_txt);
397 if(resp != 2) { 401 if(resp != 2) {
398 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 402 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
399 close_f(stream); 403 close_f(stream);
400 return STREAM_ERROR; 404 return STREAM_ERROR;
404 close_f(stream); 408 close_f(stream);
405 return STREAM_ERROR; 409 return STREAM_ERROR;
406 } 410 }
407 411
408 // Set the transfert type 412 // Set the transfert type
409 resp = FtpSendCmd("TYPE I\r\n",p,rsp_txt); 413 resp = FtpSendCmd("TYPE I",p,rsp_txt);
410 if(resp != 2) { 414 if(resp != 2) {
411 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'TYPE I' failed: %s\n",rsp_txt); 415 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'TYPE I' failed: %s\n",rsp_txt);
412 close_f(stream); 416 close_f(stream);
413 return STREAM_ERROR; 417 return STREAM_ERROR;
414 } 418 }
415 419
416 // Get the filesize 420 // Get the filesize
417 snprintf(str,255,"SIZE %s\r\n",p->filename); 421 snprintf(str,255,"SIZE %s",p->filename);
418 resp = FtpSendCmd(str,p,rsp_txt); 422 resp = FtpSendCmd(str,p,rsp_txt);
419 if(resp != 2) { 423 if(resp != 2) {
420 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 424 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
421 } else { 425 } else {
422 int dummy; 426 int dummy;
429 close_f(stream); 433 close_f(stream);
430 return STREAM_ERROR; 434 return STREAM_ERROR;
431 } 435 }
432 436
433 // Get the file 437 // Get the file
434 snprintf(str,255,"RETR %s\r\n",p->filename); 438 snprintf(str,255,"RETR %s",p->filename);
435 resp = FtpSendCmd(str,p,rsp_txt); 439 resp = FtpSendCmd(str,p,rsp_txt);
436 440
437 if(resp != 1) { 441 if(resp != 1) {
438 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); 442 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
439 close_f(stream); 443 close_f(stream);